"use strict";const t=Promise,e=globalThis.window,a=clearInterval,s=t=>null==t,r=(t,e,a)=>s(t)?a?.():e(t),n=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)&&r(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)?(r=t,n=e,r?.delete(n),t):t?.set(e,a);var r,n},C=(t,e,a,s)=>{var r,n;return r=t,n=e,r?.has(n)||A(t,e,a()),b(t,e)},S=v(),f=v(),D=["t","v"],M={keyPath:"k"},x=async(t,e)=>{const a=(e=>l(g(e),(([e,a])=>j(t,"put",{k:e,v:a}))))(e);l(await j(t,"getAllKeys"),(s=>((t,e)=>e in t)(e,s)?0:y(a,j(t,"delete",s)))),await c(a)},j=async(t,e,a)=>o(((s,r)=>{const n=t[e](a);n.onsuccess=()=>s(n.result),n.onerror=()=>r(`objectStore.${e} error`)}));exports.createIndexedDbPersister=(t,u,g=1,w)=>{const v=async(t,a=[],s=0)=>o(((r,n)=>{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,r)=>await t(e.objectStore(s),a[r]))));o.result.close(),r(s)}catch(t){o.result.close(),n(t)}},o.onerror=()=>n("indexedDB.open error")}));return((t,e,a,o,c,l,d,u={},g=[])=>{let w,v,D,M=0,x=0,j=0;C(S,g,(()=>0)),C(f,g,(()=>[]));const[k,L,P,m,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=>{(k&&n(e?.[0])?1===e?.[2]?t.applyMergeableChanges:t.setMergeableContent:1===e?.[2]?t.applyChanges:t.setContent)(e)},I=async t=>(2!=M&&(M=1,x++,await F((async()=>{try{const t=await e();n(t)?B(t):i("Content is not an array "+t)}catch(e){l?.(e),t&&T(t)}M=0}))),K),O=()=>(v&&(c(v),v=void 0),K),E=async t=>(1!=M&&(M=2,j++,await F((async()=>{try{await a(L,t)}catch(t){l?.(t)}M=0}))),K),z=()=>(r(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:I,startAutoLoad:async t=>{await O().load(t);try{v=await o((async(t,e)=>{e||t?2!=M&&(M=1,x++,B(e??t),M=0):await I()}))}catch(t){l?.(t)}return K},stopAutoLoad:O,isAutoLoading:()=>!s(v),save:E,startAutoSave:async()=>(await z().save(),D=t.addDidFinishTransactionListener((()=>{const t=P();m(t)&&E(t)})),K),stopAutoSave:z,isAutoSaving:()=>!s(D),schedule:F,getStore:()=>t,destroy:()=>(b(f,g).splice(0,void 0),O().stopAutoSave()),getStats:()=>({loads:x,saves:j}),...u};return p(K)})(t,(async()=>await v((async t=>((t=[])=>d.fromEntries(t))(l(await j(t,"getAll"),(({k:t,v:e})=>[t,e])))))),(async t=>await v((async(t,e)=>await x(t,e)),t(),1)),(t=>setInterval(t,1e3*g)),(t=>a(t)),w,1,{getDbName:()=>u})},exports.objectStoreMatch=x;
