Liferay.Loader.define("frontend-js-spa-web@5.0.38/screen/RequestScreen",["module","exports","require","frontend-js-web","../util/utils","./Screen"],(function(e,t,r){window;{Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var s=r("frontend-js-web"),o=r("../util/utils"),n=function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}(r("./Screen"));function ownKeys(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 _defineProperty(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",u="Failed to fetch",a="NetworkError when attempting to fetch resource.",c="Preflight response is not successful",h="Request error",d="Request timeout",l="Request terminated prematurely";class RequestScreen extends n.default{constructor(){super(),this.cacheable=!0,this.httpHeaders={"X-PJAX":"true","X-Requested-With":"XMLHttpRequest"},this.httpMethod=RequestScreen.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,o.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,o=this.httpMethod;const n=function _objectSpread(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(Object(r),!0).forEach((function(t){_defineProperty(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):ownKeys(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__),o=RequestScreen.POST);const i=this.formatLoadPath(e);return this.setRequest({method:o,requestBody:r,requestHeaders:n,url:i}),Promise.race([(0,s.fetch)(i,{body:r,headers:n,method:o,mode:"cors",redirect:"follow",referrer:"client"}).then((e=>(this.assertValidResponseStatusCode(e.status),this.setResponse(e),e.clone().text()))).then((e=>(o===RequestScreen.GET&&this.isCacheable()&&this.addCache(e),e))),new Promise(((e,t)=>{setTimeout((()=>t(new Error(d))),this.timeout)}))]).catch((e=>{switch(e.message){case d:e.timeout=!0;break;case l:case u:case a:case c:e.requestError=!0,e.requestPrematureTermination=!0;break;case h: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}}RequestScreen.GET="get",RequestScreen.POST="post";var i=RequestScreen;t.default=i}}));
//# sourceMappingURL=RequestScreen.js.map