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