Liferay.Loader.define("frontend-js-spa-web@5.0.32/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"),i=(s=r("./Screen"))&&s.__esModule?s:{default:s};function a(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",h="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 i.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)){const r=new Error(e);throw r.invalidStatus=!0,r.statusCode=t,r}}beforeUpdateHistoryPath(e){const t=this.getRequestPath();return t&&t!==e?t:e}beforeUpdateHistoryState(e){return e.senna&&e.form&&e.redirectPath===e.path?null:e}formatLoadPath(e){const 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(){const e=this.getRequest();if(e){let t=e.url;const r=this.getResponse();if(r){const e=r.url;e&&(t=e)}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?a(Object(r),!0).forEach((function(t){u(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):a(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 i=this.formatLoadPath(e);return this.setRequest({method:s,requestBody:r,requestHeaders:n,url:i}),Promise.race([(0,o.fetch)(i,{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 h: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 c=w;t.default=c}}));
//# sourceMappingURL=RequestScreen.js.map