function w(e,t=""){let s={};return Object.keys(e).forEach(n=>{e[n]===null||e[n]===!1?delete e[n]:typeof e[n]=="object"?s=Object.assign(s,w(e[n],`${t}[${n}]`)):s[`${t}[${n}]`]=e[n]}),s}const l=function(e,t,s={}){const n=t.startsWith("/")?t.slice(1):t,p=`${window.fluentCartRestVars.rest.url}/${n}`,i={"X-WP-Nonce":window.fluentCartRestVars.rest.nonce};return["PUT","PATCH","DELETE"].indexOf(e.toUpperCase())!==-1&&(i["X-HTTP-Method-Override"]=e,e="POST"),s.query_timestamp=Date.now(),new Promise((a,c)=>{const o=new XMLHttpRequest;let f=p;if(e.toUpperCase()==="GET"){let r={};Object.keys(s).forEach(u=>{s[u]===null||s[u]===!1?delete s[u]:typeof s[u]=="object"?r=Object.assign(r,w(s[u],u)):r[u]=s[u]});let d=new URLSearchParams(r).toString();f.indexOf("?")!==-1?f+="&"+d:f+="?"+d}o.open(e,f,!0),Object.keys(i).forEach(r=>{o.setRequestHeader(r,i[r])}),o.onload=function(){let r;try{r=JSON.parse(o.responseText)}catch{r=null}this.status>=200&&this.status<300?a(r):(r&&(r.code==="rest_cookie_invalid_nonce"||r.code==="rest_cookie_invalid")&&document.dispatchEvent(new CustomEvent("fcart_renew_rest_nonce",{detail:r})),c({data:r,status_code:o.status}))},o.onerror=function(){console.info("Your server firewall blocked the request or it's a plugin conflict. Please check the detailed error."),console.log({status:o.status,statusText:o.statusText,responseText:o.responseText}),c({status:o.status,statusText:o.statusText})},e.toUpperCase()==="GET"?o.send():(o.setRequestHeader("Content-Type","application/json;charset=UTF-8"),o.send(JSON.stringify(s)))})},T={get(e,t={}){return l("GET",e,t)},post(e,t={}){return l("POST",e,t)},delete(e,t={}){return l("DELETE",e,t)},put(e,t={}){return l("PUT",e,t)},patch(e,t={}){return l("PATCH",e,t)},upload(e,t={}){const s=`${window.fluentCartRestVars.rest.url}/${e}`,n={"X-WP-Nonce":window.fluentCartRestVars.rest.nonce};return new Promise((p,i)=>{const a=new XMLHttpRequest;a.open("POST",s,!0),Object.keys(n).forEach(c=>{a.setRequestHeader(c,n[c])}),a.onload=function(){this.status>=200&&this.status<300?p(JSON.parse(a.responseText)):i(JSON.parse(a.responseText))},a.onerror=function(){i({status:a.status,statusText:a.statusText})},a.send(t)})},getNonce(){return window.fluentCartRestVars.rest.nonce}};window.jQuery!==void 0&&jQuery(document).ajaxSuccess((e,t,s)=>{const n=t.getResponseHeader("X-WP-Nonce");n&&(window.fluentCartRestVars,window.fluentCartRestVars.rest_nonce=n)});export{T as R};
