import{t as f}from"./toolbox.D5G-ZeQK.js";import"./_sentry-release-injection-file.BmNkgrnS.js";import{u,p as $}from"./client.mqYUBE5A.js";import{c as S}from"./client-config.Bo_--JVb.js";import"./entry.DYMwKZI8.js";import{LoggerFactory as v}from"./browser-debug-logger.CMi6BVTC.js";import{z as k}from"./index.DS92w8-C.js";import"./schema.C5F4h5X5.js";import"./bson.BDI9zZYp.js";import"./index.J1NSt9Zf.js";import{c as m}from"./exports.CxSsGlop.js";(function(){try{var n=typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},r=new n.Error().stack;r&&(n._sentryDebugIds=n._sentryDebugIds||{},n._sentryDebugIds[r]="421e2eac-3a7d-4b7e-97ac-1b246817dc4d",n._sentryDebugIdIdentifier="sentry-dbid-421e2eac-3a7d-4b7e-97ac-1b246817dc4d")}catch{}})();function F(n,r,e){try{Array.isArray(e)?n.set(r,e.join(",")):e===null?n.set(r,""):n.set(r,`${e}`)}catch(t){throw new Error(`Error in URLSearchParams: ${f.errorToString(t)}`,{cause:t})}}function b(n){try{const r=new URLSearchParams;return Object.entries(n).forEach(([e,t])=>{F(r,e,t)}),r}catch(r){throw new Error(`Error in encodeQueryParamsSchema: ${f.errorToString(r)}`,{cause:r})}}function h(n){const{path:r,qp:e,params:t,formAction:o}=n;try{let a=r;if(t){const p=typeof t=="function"?t():t;for(const l in p){const d=new RegExp(`\\[\\[?${l}\\]?\\]`,"g");a=a.replaceAll(d,`${p[l]}`)}}a=a.replaceAll(/\[\[.*?\]\]\/?/g,""),a=a.replace(/\/$/,"");const i=b(e||{});return`${a}?${o&&o!="default"?`/${o}${i.size>0?"&":""}${i}`:`${i}`}`}catch(a){throw new Error(`Error in querySdkUrlHelper: ${f.errorToString(a)}`,{cause:a})}}const D=k.object({});function _(n){const{path:r}=n;return{queryWithSvelteFetchAndUnpack:async e=>(await A({svelteKitFetch:e.svelteKitFetch,url:h({path:r,qp:e.qp,params:e.params})})).parsedBody,queryWithSvelteFetch:e=>x({svelteKitFetch:e.svelteKitFetch,url:h({path:r,qp:e.qp,params:e.params})}),queryClient:e=>w({url:h({path:r,qp:e.qp,params:e.params})}),queryClientSimple:e=>g({url:h({path:r,qp:e.qp,params:e.params}),abortSignal:e.abortSignal}),queryClientSimpleLoadFn:async e=>g({svelteKitFetch:e.svelteKitFetch,url:h({path:r,qp:e.qp,params:e.params})}),unpack:e=>e instanceof Promise?e.then(t=>t.type==="error"?t:{type:"success",data:u(t.data)}):e.type==="error"?e:{type:"success",data:u(e.data)},unpackAndThrow:e=>{if(e instanceof Promise)return e.then(t=>{if(t.type==="error")throw new Error(t.error.message);return u(t.data)});if(e.type==="error")throw new Error(e.error.message);return u(e.data)}}}async function A(n){let r="",e,t,o,a,i;try{const{svelteKitFetch:s}=n;e=await s(n.url,{headers:{Accept:"application/json;devalue"}}),i=e.headers.get("content-type")||e.headers.get("Content-Type")||"",t=e.headers.get(S.VERSION_HEADER),o=i.includes("application/json"),a=i.includes("application/devalue"),r=await e.text()}catch(s){throw new Error(`queryServerAndUnpack@${f.errorToString(s)} - response was(${r})`,{cause:s})}if(!e.ok&&t&&o){const s=JSON.parse(r);throw new Error(s.message)}if(!e.ok&&t&&!o)throw new Error(`
                queryServerAndUnpack on url '${n.url}' failed unexpected with '${r}' (expected JSON)
            `);if(!e.ok&&!t)throw new Error(`
                queryServerAndUnpack on url '${n.url}' got no response from our server! Failed with '${r}'
            `);if(o)return{response:e,parsedBody:JSON.parse(r)};if(a)return{response:e,parsedBody:u(r)};throw new Error(`Expected application/devalue, got ${i}`)}async function x(n){const{svelteKitFetch:r,url:e}=n,t=await r(e,{headers:{Accept:"application/json;devalue"}}),o=t.headers.get("content-type")||t.headers.get("Content-Type"),a=await t.text();if(!t.ok)return{type:"error",error:JSON.parse(a),data:null};if(o!=null&&o.includes("application/json"))return{type:"success",data:$(JSON.parse(a))};if(o!=null&&o.includes("application/devalue"))return{data:a,type:"success"};throw new Error("query-sdk-helpers@queryServer Can this happen?")}async function w(n,r=0){const{url:e,svelteKitFetch:t,abortSignal:o}=n;r+=1;const a=2,{logger:i}=new v("query-client");let s=null;try{s=await(t||fetch)(e,{method:"GET",headers:{Accept:"application/json;devalue"},signal:o})}catch(c){i.log(`Network error/"Failed to fetch". ${e}. Error was '${f.errorToString(c)}'.`,{error:c})()}if(!s)return r<=a?(i.log(`No response! AttemptCount (${r}) <= limit (${a}). Will retry`)(),w(n,r)):{type:"error",error:{message:"Network error"},data:null};const p=s.headers.get("content-type")||s.headers.get("Content-Type")||"",l=s.headers.get(S.VERSION_HEADER),d=p.includes("application/json"),E=p.includes("application/devalue"),y=await s.text();if(!s.ok&&l&&d){const c=JSON.parse(y);if(!(c!=null&&c.errId)){const q=m(new Error(`query-client.ts on url '${e}' failed with '${c.message}'`));c.errId=q}return{type:"error",error:c,data:null}}if(!s.ok&&l&&!d)return{type:"error",error:{message:"Unexpected error",errId:m(new Error(`query-client on url '${e}' failed unexpected with '${y}' (expected JSON)`))},data:null};if(!s.ok&&!l)return{type:"error",error:{message:"Unexpected network error",errId:m(new Error(`query-client on url '${e}' got no response from our server! Failed with '${y}'`))},data:null};if(d)return{type:"success",data:JSON.parse(y)};if(E)return{data:u(y),type:"success"};throw new Error("queryClient@Unexpected content-type")}async function g(n){const r=await w(n);if(r.type==="error")throw new Error(r.error.message);if(r.type==="success")return r.data;throw new Error("queryClientSimple@Should not happen...")}export{F as a,w as b,b as c,D as e,_ as g,h as q};
//# sourceMappingURL=query-sdk-helpers.CPfHMa0B.js.map
