const t=Promise,e=globalThis.window,a=clearInterval,n=t=>null==t,s=(t,e,a)=>n(t)?a?.():e(t),r=t=>Array.isArray(t),o=t=>t.length,i=e=>new t(e),c=async e=>t.all(e),l=t=>{throw Error(t)},u=async(t,e,a)=>{try{return await t()}catch(t){e?.(t)}},d=(t,e)=>t.forEach(e),y=(t,e)=>t.map(e),w=(t,...e)=>t.push(...e),g=t=>t.shift(),p=Object,v=t=>p.getPrototypeOf(t),h=p.entries,b=p.keys,A=p.freeze,S=t=>(t=>!n(t)&&s(v(t),t=>t==p.prototype||n(v(t)),()=>!0))(t)&&0==(t=>o(b(t)))(t),C=t=>n(t)||0==(t=>t?.size??0)(t),f=(t,e)=>t?.forEach(e),L=(t,e)=>t?.delete(e),D=t=>new Map(t),M=(t,e)=>t?.get(e),P=(t,e,a)=>n(a)?(L(t,e),t):t?.set(e,a),k=(t,e,a,n)=>{var s,r;return s=t,r=e,s?.has(r)?n?.(M(t,e)):P(t,e,a()),M(t,e)},j=(t,e,a,n,r=0)=>s((a?k:M)(t,e[r],r>o(e)-2?a:D),s=>{if(r>o(e)-2)return n?.(s)&&P(t,e[r]),s;const i=j(s,e,a,n,r+1);return C(s)&&P(t,e[r]),i}),m=/^\d+$/,x=t=>new Set(r(t)||n(t)?t:[t]),E=D(),T=D(),B=(t,e,a,i,c,y,p,v={},h=0,b=[])=>{let B,O,z,I=0,$=0,F=0;k(E,b,()=>0),k(T,b,()=>[]);const K=D(),[N,q,G,H,J]=((t=1,e,a)=>1!=t&&e.isMergeable()?[1,e.getMergeableContent,()=>e.getTransactionMergeableChanges(!a),([[t],[e]])=>!S(t)||!S(e),e.setDefaultContent]:2!=t?[0,e.getContent,e.getTransactionChanges,([t,e])=>!S(t)||!S(e),e.setContent]:l("Store type not supported by this Persister"))(p,t,h),[Q,R,U]=(()=>{let t;const[e,a]=(()=>{const t=[];let e=0;return[a=>(a?g(t):null)??""+e++,e=>{m.test(e)&&o(t)<1e3&&w(t,e)}]})(),r=D();return[(a,n,s,o=[],i=()=>[])=>{t??=st;const c=e(1);var l,u;return P(r,c,[a,n,s,o,i]),l=j(n,s??[""],x),u=c,l?.add(u),c},(e,a,...n)=>d(((t,e=[""])=>{const a=[],n=(t,s)=>s==o(e)?w(a,t):null===e[s]?f(t,t=>n(t,s+1)):d([e[s],null],e=>n(M(t,e),s+1));return n(t,0),a})(e,a),e=>f(e,e=>M(r,e)[0](t,...a??[],...n))),t=>s(M(r,t),([,e,n])=>(j(e,n??[""],void 0,e=>(L(e,t),C(e)?1:0)),P(r,t),a(t),n)),e=>s(M(r,e),([e,,a=[],s,r])=>{const i=(...c)=>{const l=o(c);l==o(a)?e(t,...c,...r(c)):n(a[l])?d(s[l]?.(...c)??[],t=>i(...c,t)):i(...c,a[l])};i()})]})(),V=t=>{t!=I&&(I=t,R(K,void 0,I))},W=e=>{(N&&r(e?.[0])?1===e?.[2]?t.applyMergeableChanges:t.setMergeableContent:1===e?.[2]?t.applyChanges:t.setContent)(e)},X=async t=>(2!=I&&(V(1),$++,await nt(async()=>{await u(async()=>{const a=await e();r(a)?W(a):t?J(t):l("Content is not an array: "+a)},()=>{t&&J(t)}),V(0)})),st),Y=async t=>(Z(),await X(t),await u(async()=>O=await i(async(t,e)=>{e||t?2!=I&&(V(1),$++,W(e??t),V(0)):await X()}),y),st),Z=async()=>(O&&(await u(()=>c(O),y),O=void 0),st),_=async t=>(1!=I&&(V(2),F++,await nt(async()=>{await u(()=>a(q,t),y),V(0)})),st),tt=async()=>(et(),await _(),z=t.addDidFinishTransactionListener(()=>{const t=G();H(t)&&_(t)}),st),et=async()=>(z&&(t.delListener(z),z=void 0),st),at=async(t=!1)=>{const[e,a]=t?[et,Z]:[Z,et];return await e(),await a(),st},nt=async(...t)=>(w(M(T,b),...t),await(async()=>{if(!M(E,b)){for(P(E,b,1);!n(B=g(M(T,b)));)await u(B,y);P(E,b,0)}})(),st),st={load:X,startAutoLoad:Y,stopAutoLoad:Z,isAutoLoading:()=>!n(O),save:_,startAutoSave:tt,stopAutoSave:et,isAutoSaving:()=>!n(z),startAutoPersisting:async(t,e=!1)=>{const[a,n]=e?[tt,Y]:[Y,tt];return await a(t),await n(t),st},stopAutoPersisting:at,getStatus:()=>I,addStatusListener:t=>Q(t,K),delListener:e=>(U(e),t),schedule:nt,getStore:()=>t,destroy:()=>(M(T,b).splice(0,void 0),at()),getStats:()=>({loads:$,saves:F}),...v};return A(st)},O=["t","v"],z={keyPath:"k"},I=async(t,e)=>{const a=(e=>y(h(e),([e,a])=>$(t,"put",{k:e,v:a})))(e);y(await $(t,"getAllKeys"),n=>((t,e)=>e in t)(e,n)?0:w(a,$(t,"delete",n))),await c(a)},$=async(t,e,a)=>i((n,s)=>{const r=t[e](a);r.onsuccess=()=>n(r.result),r.onerror=()=>s(`objectStore.${e} error`)}),F=(t,n,s=1,r)=>{const o=async(t,a=[],s=0)=>i((r,o)=>{const i=(e?e.indexedDB:indexedDB).open(n,s?2:void 0);i.onupgradeneeded=()=>s&&y(O,t=>u(()=>i.result.createObjectStore(t,z))),i.onsuccess=()=>u(async()=>{const e=i.result.transaction(O,"readwrite"),n=await c(y(O,(n,s)=>t(e.objectStore(n),a[s])));i.result.close(),r(n)},t=>{i.result.close(),o(t)}),i.onerror=()=>o("indexedDB.open error")});return B(t,async()=>await o(async t=>((t=[])=>p.fromEntries(t))(y(await $(t,"getAll"),({k:t,v:e})=>[t,e]))),t=>o((t,e)=>I(t,e),t(),1),t=>setInterval(t,1e3*s),t=>a(t),r,1,{getDbName:()=>n})};export{F as createIndexedDbPersister,I as objectStoreMatch};
