function e({original:e,fallback:t}){let n=e.map(({slug:e})=>e?.current),r=t.filter(({slug:e})=>!n.includes(e?.current));return[...e,...r]}var t=({query:e,params:t={},options:n={}})=>{let r=new URLSearchParams,{tag:i,...a}=n;i&&r.set(`tag`,i),r.set(`query`,e);for(let[e,n]of Object.entries(t))r.set(`$${e}`,JSON.stringify(n));for(let[e,t]of Object.entries(a))t&&r.set(e,`${t}`);return`?${r.toString()}`},n=({dataset:e,projectId:t,apiVersion:n,query:r=``})=>`https://${t}.apicdn.sanity.io/${n}/data/query/${e}${r}`,r=e=>{let t={_type:`listGroup`,listTag:null,listBlocks:[]},n=[];if(e?.length)for(let r=0;re.filter(e=>e?.slug?.current&&!i.includes(e.slug.current)),o=2e3,s=3e4,c=o*5,l=new Map;function u(e){let t=l.get(e);return t||(t={status:`closed`,failures:[],openUntil:0},l.set(e,t)),t}function d(e,t){return e.status===`open`?t>=e.openUntil?(e.status=`half-open`,!1):!0:e.status===`half-open`}function f(e,t,n){e.status=`open`,e.openUntil=n+s,_({source:`sanity-client`,msg:`sanity circuit breaker opened`,operationName:t,failureCount:e.failures.length,cooldownMs:s})}function p(e){e.status===`half-open`&&(e.status=`closed`,e.failures=[],e.openUntil=0)}function m(e,t,n){e.status===`half-open`?f(e,t,n):(e.failures=e.failures.filter(e=>n-e=5&&f(e,t,n))}var h=class extends Error{constructor(e,t){super(e,t)}};function g(e){return e instanceof h}function _(e){console.error(JSON.stringify(e,(e,t)=>t instanceof Error?{name:t.name,message:t.message,stack:t.stack,cause:t.cause}:t))}var v=new class{cache=new Map;maxEntries;constructor(e){this.maxEntries=e}get size(){return this.cache.size}async get(e){let t=this.cache.get(e);if(!t)return null;let n=Date.now();return t.expiresAt<=n?(this.cache.delete(e),null):(this.cache.delete(e),this.cache.set(e,t),t)}async set(e,t){for(this.cache.has(e)&&this.cache.delete(e),this.cache.set(e,t);this.cache.size>this.maxEntries;){let e=this.cache.keys().next().value;if(e===void 0)break;this.cache.delete(e)}}async delete(e){this.cache.delete(e)}async clearExpired(){let e=Date.now();for(let[t,n]of this.cache)n.expiresAt<=e&&this.cache.delete(t)}async clearAll(){this.cache.clear()}}(500),y=new Map;function b(e){return y.get(e)}async function x(e,t,n){let r=y.get(e)??{value:t,count:0};r.count+=1,y.set(e,r);try{await n()}catch{}finally{--r.count,r.count===0&&y.delete(e)}}var S=class{dataset;projectId;apiVersion;defaultLanguage;isAuthenticated;cache;constructor(e){this.dataset=e.dataset,this.projectId=e.projectId,this.apiVersion=e.apiVersion,this.defaultLanguage=e.defaultLanguage,this.isAuthenticated=e.isAuthenticated,this.cache=e.cache}unauthenticatedQuery(e,t){let n=this.createQuery(e,t);return async(e,t)=>this.isAuthenticated()?null:n(e,t)}createQuery(e,r){let i=Array(e).join(``);return async(e,a)=>{let s=e?.params?.lang??this.defaultLanguage,c=null,l=new AbortController,f=l.signal,y=t({query:i,params:{lang:s,defaultLanguage:this.defaultLanguage,...a}}),S=new URL(n({dataset:this.dataset,projectId:this.projectId,apiVersion:this.apiVersion,query:y})).href,C={method:`GET`,headers:{"Access-Control-Allow-Origin":`*`,"Content-Type":`application/json`,"x-sanity-operation":r?.operationName?`Sanity${r.operationName}`:`SanityQuery`},signal:f},w=typeof window<`u`,T=r?.operationName??`default`,E=u(T);try{let t=Date.now();if(r?.cache?.enabled){let t=await this.cache.get(S);if(t)return!w&&this.cache===v&&await x(S,t.value,()=>e.fetch(S,C).then(e=>e.text())),t.value}if(!w&&d(E,t))return null;w||(c=setTimeout(()=>{l.abort()},o));let n=b(S)!==void 0,i;try{i=await e.fetch(S,C)}catch(e){throw new h(e instanceof Error?e.message:String(e),{cause:e instanceof Error?e.cause:e})}if(i.status>=500)throw i?.body?.cancel(),new h(`sanity: response status is `+i.status,{cause:{status:i.status}});let a=await i.json();if(`error`in a)throw Error(`sanity: error found in json`,{cause:{sanityError:a.error}});if(i.status!==200)throw Error(`sanity: response status is `+i.status,{cause:{status:i.status}});if(!(`result`in a))throw Error(`sanity: no result json in`,{cause:{status:i.status}});let s=a.result;if(r?.cache?.enabled&&s!=null&&!n){let e=t+r.cache.ttlMs;await this.cache.set(S,{value:s,expiresAt:e})}return w||p(E),s}catch(e){return _({source:`sanity-client`,msg:`sanity fetch error`,operationName:r?.operationName,url:S,error:e}),w||(g(e)?m(E,T,Date.now()):E.status===`half-open`&&p(E)),null}finally{c&&clearTimeout(c)}}}},C=null;function w(e){return new S(e)}function T(e){C&&console.warn(`Default Sanity client already initialized`),C=w(e)}function E(){if(!C)throw Error(`Sanity client not initialized. Call initializeSanityClient() first.`);return C}function D(e,t){return(n,r)=>E().createQuery(e,t)(n,r)}export{e as a,v as i,E as n,a as o,T as r,r as s,D as t};