(function(a,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(a=typeof globalThis<"u"?globalThis:a||self,n(a.ZustandQueries={}))})(this,function(a){"use strict";function n(P){let y=(c,u)=>u?Object.setPrototypeOf(u,c):c,C=y({autofetch:!0,lifetime:3e5},P);return(c,u)=>{let $=new WeakMap,m=JSON.stringify,w=()=>c(e=>({$cache:new Map(e.$cache)})),h=e=>u().$cache.get(e)??u().$cache.set(e,new Map).get(e),d=(e,t,i)=>{let r=h(e),f=r.get(t),l=$.get(f.refetch);l[0]&&clearTimeout(l[0]),r.set(t,{...f,...i}),l[0]=setTimeout(()=>{r.delete(t),w()},l[1]),w()},p=(e,t=[],i,r=C)=>{let f=0,l=()=>e(...t).then(o=>(d(e,i,{data:o,loading:!1}),o),o=>{r.retry&&++f<5?setTimeout(l,2**f*1e3):d(e,i,{error:o,loading:!1})}),s=l();return d(e,i,{promise:s,loading:!0}),s},T=(e,t,i=[],r)=>{let f=y(C,r),l=h(t),s=m(i);if(!l.has(s)){let g=()=>p(t,i,s,f);l.set(s,{promise:Promise.resolve(),refetch:g}),$.set(g,[0,f.lifetime]),f.autofetch&&g()}let o=l.get(s);if(e){if("error"in o)throw o.error;if(!("data"in o))throw o.promise}return o};return{$cache:new Map,$refetch(e,t=[]){let i=m(t);return h(e).has(i)?p(e,t,i):Promise.reject(new Error("query not found"))},$invalidate(e,t=[],i){let r=m(t);h(e).has(r)&&(i?d(e,r,{data:i,loading:!1}):p(e,t,r))},$suspenseQuery:(e,t=[],i)=>T(!0,e,t,i),$query:(e,t=[],i)=>T(!1,e,t,i)}}}a.createCache=n,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});