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