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