Liferay.Loader.define("frontend-js-spa-web@5.0.24/screen/RequestScreen",["module","exports","require","frontend-js-web","../util/utils","./Screen"],(function(e,t,r){var s;window;{Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var o=r("frontend-js-web"),n=r("../util/utils"),a=(s=r("./Screen"))&&s.__esModule?s:{default:s};function i(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var s=Object.getOwnPropertySymbols(e);t&&(s=s.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,s)}return r}function u(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}const e="Invalid status code",c="Failed to fetch",d="NetworkError when attempting to fetch resource.",l="Preflight response is not successful",p="Request error",f="Request timeout",m="Request terminated prematurely";class w extends a.default{constructor(){super(),this.cacheable=!0,this.httpHeaders={"X-PJAX":"true","X-Requested-With":"XMLHttpRequest"},this.httpMethod=w.GET,this.request=null,this.timeout=3e4}assertValidResponseStatusCode(t){if(!this.isValidResponseStatusCode(t)){var r=new Error(e);throw r.invalidStatus=!0,r.statusCode=t,r}}beforeUpdateHistoryPath(e){var t=this.getRequestPath();return t&&t!==e?t:e}beforeUpdateHistoryState(e){return e.senna&&e.form&&e.redirectPath===e.path?null:e}formatLoadPath(e){var t=new URL(e,window.location.origin);return t.hostname=window.location.hostname,t.protocol=window.location.protocol,window.location.port&&(t.port=window.location.port),t.toString()}getHttpHeaders(){return this.httpHeaders}getHttpMethod(){return this.httpMethod}getRequestPath(){var e=this.getRequest();if(e){var t=e.url,r=this.getResponse();if(r){var s=r.url;s&&(t=s)}return(0,n.getUrlPath)(t)}return null}getResponse(){return this.response}getRequest(){return this.request}getTimeout(){return this.timeout}isValidResponseStatusCode(e){return e>=200&&e<=399}getFormData(e,t){const r=new FormData(e);return this.maybeAppendSubmitButtonValue_(r,t),r}load(e){const t=this.getCache();if(t)return Promise.resolve(t);let r=null,s=this.httpMethod;const n=function(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?i(Object(r),!0).forEach((function(t){u(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):i(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}({"X-PJAX":"true"},this.httpHeaders);Liferay.SPA.__capturedFormElement__&&(r=this.getFormData(Liferay.SPA.__capturedFormElement__,Liferay.SPA.__capturedFormButtonElement__),s=w.POST);const a=this.formatLoadPath(e);return this.setRequest({method:s,requestBody:r,requestHeaders:n,url:a}),Promise.race([(0,o.fetch)(a,{body:r,headers:n,method:s,mode:"cors",redirect:"follow",referrer:"client"}).then((e=>(this.assertValidResponseStatusCode(e.status),this.setResponse(e),e.clone().text()))).then((e=>(s===w.GET&&this.isCacheable()&&this.addCache(e),e))),new Promise(((e,t)=>{setTimeout((()=>t(new Error(f))),this.timeout)}))]).catch((e=>{switch(e.message){case f:e.timeout=!0;break;case m:case c:case d:case l:e.requestError=!0,e.requestPrematureTermination=!0;break;case p:default:e.requestError=!0}throw e}))}maybeAppendSubmitButtonValue_(e,t){t&&t.name&&e.append(t.name,t.value)}setHttpHeaders(e){this.httpHeaders=e}setHttpMethod(e){this.httpMethod=e.toLowerCase()}setRequest(e){this.request=e}setResponse(e){this.response=e}setTimeout(e){this.timeout=e}}w.GET="get",w.POST="post";var h=w;t.default=h}}));
//# sourceMappingURL=RequestScreen.js.map