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