import{a as apiFetch}from"./index-824c562b.js";import{a as addQueryArgs}from"./add-query-args-0e2a8393.js";import{g as getQueryArg}from"./remove-query-args-938c53ea.js";var _a,_b,_c,_d,_e,_f,_g;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=wp.i18n.__("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:wp.i18n.sprintf(
/* translators: %s: URL to debug settings page */
wp.i18n.__("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">${wp.i18n.__("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=wp.i18n.__("We were not able to process this order","surecart")),Promise.reject(e)})),n}));const parseJsonAndNormalizeError=e=>{const o={code:"invalid_json",message:wp.i18n.__("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}))},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}))};export{handleNonceError as h};