import{a as o}from"./p-259c3935.js";import{a as i}from"./p-401e165e.js";import{g as n}from"./p-d3366af3.js";var e,r,d,s,w,t,l;const a=(null===(r=null===(e=null===window||void 0===window?void 0:window.parent)||void 0===e?void 0:e.scData)||void 0===r?void 0:r.root_url)||(null===(d=null===window||void 0===window?void 0:window.scData)||void 0===d?void 0:d.root_url);a&&(o.fetchAllMiddleware=null,o.use(o.createRootURLMiddleware(a)),(null===(s=null===window||void 0===window?void 0:window.scData)||void 0===s?void 0:s.nonce)&&(o.nonceMiddleware=o.createNonceMiddleware(null===(w=null===window||void 0===window?void 0:window.scData)||void 0===w?void 0:w.nonce),o.use(o.nonceMiddleware)),(null===(t=null===window||void 0===window?void 0:window.scData)||void 0===t?void 0:t.nonce_endpoint)&&(o.nonceEndpoint=null===(l=null===window||void 0===window?void 0:window.scData)||void 0===l?void 0:l.nonce_endpoint),o.use(((o,e)=>(o.path=i(o.path,{t:Date.now()}),e(o)))),o.use(((o,e)=>(o.path=i(o.path,{...!!n(window.location.href,"currency")&&{currency:n(window.location.href,"currency")}}),e(o)))),o.use(((o,e)=>{const n=e(o);return n.catch((o=>{var e;if("invalid_json"===o.code){o.message=wp.i18n.__("The response is not a valid JSON response.","surecart");const e="https://surecart.com/docs/is-not-a-valid-json-response/";o.additional_errors=[{code:"invalid_json",message:wp.i18n.sprintf(wp.i18n.__("Please ensure that your site is not in debug mode as this may interfere with API responses. %s","surecart"),`<a href="${e}" target="_blank" rel="noopener noreferrer">${wp.i18n.__("More Information","surecart")}</a>`)}]}return"checkout.finalize_error"===o.code&&(o.additional_errors=[...(null===(e=null==o?void 0:o.additional_errors)||void 0===e?void 0:e.length)?[]:[{code:"checkout.finalize_error",message:o.message}],...o.additional_errors||[]],o.message=wp.i18n.__("We were not able to process this order","surecart")),Promise.reject(o)})),n})));const v=o=>{const e={code:"invalid_json",message:wp.i18n.__("The response is not a valid JSON response.","surecart")};if((null==o?void 0:o.code)&&(null==o?void 0:o.message))throw o;if(!o||!o.json)throw e;return o.json().catch((()=>{throw e}))},u=async e=>{const n=await v(e);if("rest_cookie_invalid_nonce"!==n.code)throw n;return window.fetch(o.nonceEndpoint).then((o=>{if(o.status>=200&&o.status<300)return o;throw o})).then((o=>o.text())).then((e=>{o.nonceMiddleware.nonce=e}))};export{u as h};