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