"use strict";const index=require("./index-7ced8198.js"),addQueryArgs=require("./add-query-args-49dcb630.js"),removeQueryArgs=require("./remove-query-args-b57e8cd3.js");var _a,_b,_c,_d,_e,_f,_g;index.apiFetch.fetchAllMiddleware=null,index.apiFetch.use(index.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)&&(index.apiFetch.nonceMiddleware=index.apiFetch.createNonceMiddleware(null===(_e=null===window||void 0===window?void 0:window.scData)||void 0===_e?void 0:_e.nonce),index.apiFetch.use(index.apiFetch.nonceMiddleware)),(null===(_f=null===window||void 0===window?void 0:window.scData)||void 0===_f?void 0:_f.nonce_endpoint)&&(index.apiFetch.nonceEndpoint=null===(_g=null===window||void 0===window?void 0:window.scData)||void 0===_g?void 0:_g.nonce_endpoint),index.apiFetch.use(((e,n)=>(e.path=addQueryArgs.addQueryArgs(e.path,{t:Date.now()}),n(e)))),index.apiFetch.use(((e,n)=>(e.path=addQueryArgs.addQueryArgs(e.path,{...!!removeQueryArgs.getQueryArg(window.location.href,"currency")&&{currency:removeQueryArgs.getQueryArg(window.location.href,"currency")}}),n(e)))),index.apiFetch.use(((e,n)=>{const o=n(e);return o.catch((e=>{var n;if("invalid_json"===e.code){e.message=wp.i18n.__("The response is not a valid JSON response.","surecart");const n="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="${n}" target="_blank" rel="noopener noreferrer">${wp.i18n.__("More Information","surecart")}</a>`)}]}return"checkout.finalize_error"===e.code&&(e.additional_errors=[...(null===(n=null==e?void 0:e.additional_errors)||void 0===n?void 0:n.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)})),o}));const parseJsonAndNormalizeError=e=>{const n={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 n;return e.json().catch((()=>{throw n}))},handleNonceError=async e=>{const n=await parseJsonAndNormalizeError(e);if("rest_cookie_invalid_nonce"!==n.code)throw n;return window.fetch(index.apiFetch.nonceEndpoint).then((e=>{if(e.status>=200&&e.status<300)return e;throw e})).then((e=>e.text())).then((e=>{index.apiFetch.nonceMiddleware.nonce=e}))};exports.handleNonceError=handleNonceError;