var _a,_b,_c,_d,_e,_f,_g;import apiFetch from"@wordpress/api-fetch";import{__,sprintf}from"@wordpress/i18n";import{addQueryArgs,getQueryArg}from"@wordpress/url";apiFetch.fetchAllMiddleware=null,apiFetch.use(apiFetch.createRootURLMiddleware((null===(_b=null===(_a=null===window||void 0===window?void 0:window.parent)||void 0===_a?void 0:_a.scData)||void 0===_b?void 0:_b.root_url)||(null===(_c=null===window||void 0===window?void 0:window.scData)||void 0===_c?void 0:_c.root_url))),(null===(_d=null===window||void 0===window?void 0:window.scData)||void 0===_d?void 0:_d.nonce)&&(apiFetch.nonceMiddleware=apiFetch.createNonceMiddleware(null===(_e=null===window||void 0===window?void 0:window.scData)||void 0===_e?void 0:_e.nonce),apiFetch.use(apiFetch.nonceMiddleware)),(null===(_f=null===window||void 0===window?void 0:window.scData)||void 0===_f?void 0:_f.nonce_endpoint)&&(apiFetch.nonceEndpoint=null===(_g=null===window||void 0===window?void 0:window.scData)||void 0===_g?void 0:_g.nonce_endpoint),apiFetch.use(((e,o)=>(e.path=addQueryArgs(e.path,{t:Date.now()}),o(e)))),apiFetch.use(((e,o)=>(e.path=addQueryArgs(e.path,{...!!getQueryArg(window.location.href,"currency")&&{currency:getQueryArg(window.location.href,"currency")}}),o(e)))),apiFetch.use(((e,o)=>{const n=o(e);return n.catch((e=>{var o;if("invalid_json"===e.code){e.message=__("The response is not a valid JSON response.","surecart");const o="https://surecart.com/docs/is-not-a-valid-json-response/";e.additional_errors=[{code:"invalid_json",message:sprintf(
/* translators: %s: URL to debug settings page */
__("Please ensure that your site is not in debug mode as this may interfere with API responses. %s","surecart"),`<a href="${o}" target="_blank" rel="noopener noreferrer">${__("More Information","surecart")}</a>`)}]}return"checkout.finalize_error"===e.code&&(e.additional_errors=[...(null===(o=null==e?void 0:e.additional_errors)||void 0===o?void 0:o.length)?[]:[{code:"checkout.finalize_error",message:e.message}],...e.additional_errors||[]],e.message=__("We were not able to process this order","surecart")),Promise.reject(e)})),n}));export default apiFetch;export const parseJsonAndNormalizeError=e=>{const o={code:"invalid_json",message:__("The response is not a valid JSON response.","surecart")};if((null==e?void 0:e.code)&&(null==e?void 0:e.message))throw e;if(!e||!e.json)throw o;return e.json().catch((()=>{throw o}))};export const handleNonceError=async e=>{const o=await parseJsonAndNormalizeError(e);if("rest_cookie_invalid_nonce"!==o.code)throw o;return window.fetch(apiFetch.nonceEndpoint).then((e=>{if(e.status>=200&&e.status<300)return e;throw e})).then((e=>e.text())).then((e=>{apiFetch.nonceMiddleware.nonce=e}))};