import*as Common from"../common/common.js";import*as i18n from"../i18n/i18n.js";import{InspectorFrontendHostInstance}from"./InspectorFrontendHost.js";const UIStrings={systemError:"System error",connectionError:"Connection error",certificateError:"Certificate error",httpError:"HTTP error",cacheError:"Cache error",signedExchangeError:"Signed Exchange error",ftpError:"FTP error",certificateManagerError:"Certificate manager error",dnsResolverError:"DNS resolver error",unknownError:"Unknown error",httpErrorStatusCodeSS:"HTTP error: status code {PH1}, {PH2}",invalidUrl:"Invalid URL",decodingDataUrlFailed:"Decoding Data URL failed"},str_=i18n.i18n.registerUIStrings("host/ResourceLoader.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export const ResourceLoader={};let _lastStreamId=0;const _boundStreams={},_bindOutputStream=function(r){return _boundStreams[++_lastStreamId]=r,_lastStreamId},_discardOutputStream=function(r){_boundStreams[r].close(),delete _boundStreams[r]};export const streamWrite=function(r,t){_boundStreams[r].write(t)};export let load=function(r,t,e){const n=new Common.StringOutputStream.StringOutputStream;loadAsStream(r,t,n,(function(r,t,o){e(r,t,n.data(),o)}))};export function setLoadForTest(r){load=r}function getNetErrorCategory(r){return i18nString(r>-100?UIStrings.systemError:r>-200?UIStrings.connectionError:r>-300?UIStrings.certificateError:r>-400?UIStrings.httpError:r>-500?UIStrings.cacheError:r>-600?UIStrings.signedExchangeError:r>-700?UIStrings.ftpError:r>-800?UIStrings.certificateManagerError:r>-900?UIStrings.dnsResolverError:UIStrings.unknownError)}function isHTTPError(r){return r<=-300&&r>-400}export function netErrorToMessage(r,t,e){if(void 0===r||void 0===e)return null;if(0!==r){if(isHTTPError(r))return i18nString(UIStrings.httpErrorStatusCodeSS,{PH1:t,PH2:e});return`${getNetErrorCategory(r)}: ${e}`}return null}function createErrorMessageFromResponse(r){const{statusCode:t,netError:e,netErrorName:n,urlValid:o,messageOverride:s}=r;let i="";const a=t>=200&&t<300;if("string"===typeof s)i=s;else if(!a)if("undefined"===typeof e)i=i18nString(!1===o?UIStrings.invalidUrl:UIStrings.unknownError);else{const r=netErrorToMessage(e,t,n);r&&(i=r)}return console.assert(a===(0===i.length)),{success:a,description:{statusCode:t,netError:e,netErrorName:n,urlValid:o,message:i}}}const loadXHR=r=>new Promise(((t,e)=>{const n=new XMLHttpRequest;n.withCredentials=!1,n.open("GET",r,!0),n.onreadystatechange=function(){if(n.readyState===XMLHttpRequest.DONE){if(200!==n.status)return n.onreadystatechange=null,void e(new Error(String(n.status)));n.onreadystatechange=null,t(n.responseText)}},n.send(null)}));export const loadAsStream=function(r,t,e,n){const o=_bindOutputStream(e);if(new Common.ParsedURL.ParsedURL(r).isDataURL())return void loadXHR(r).then((function(r){streamWrite(o,r),i({statusCode:200})})).catch((function(r){i({statusCode:404,messageOverride:i18nString(UIStrings.decodingDataUrlFailed)})}));const s=[];if(t)for(const a in t)s.push(a+": "+t[a]);function i(r){if(n){const{success:t,description:e}=createErrorMessageFromResponse(r);n(t,r.headers||{},e)}var t;_boundStreams[t=o].close(),delete _boundStreams[t]}InspectorFrontendHostInstance.loadNetworkResource(r,s.join("\r\n"),o,i)};