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