var e,t;e=this,t=function(e){"use strict";const t=globalThis.window,n=e=>null==e,o=(e,t,o)=>n(e)?null==o?void 0:o():t(e),r=e=>Array.isArray(e),l=e=>{throw Error(e)},i=Object,a=e=>i.getPrototypeOf(e),s=i.keys,u=i.freeze,d=e=>(e=>!n(e)&&o(a(e),(e=>e==i.prototype||n(a(e))),(()=>!0)))(e)&&0==(e=>s(e).length)(e),c=JSON.stringify,v=JSON.parse,y=e=>new Map(e),f=(e,t)=>null==e?void 0:e.get(t),p=(e,t,o)=>{return n(o)?(l=t,null==(r=e)||r.delete(l),e):null==e?void 0:e.set(t,o);var r,l},g=(e,t,n,o)=>{var r,l,i;return l=t,null!=(i=null==(r=e)?void 0:r.has(l))&&i||p(e,t,n()),f(e,t)};var h=Object.defineProperty,b=Object.getOwnPropertySymbols,S=Object.prototype.hasOwnProperty,m=Object.prototype.propertyIsEnumerable,w=(e,t,n)=>t in e?h(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,P=(e,t,n)=>new Promise(((o,r)=>{var l=e=>{try{a(n.next(e))}catch(e){r(e)}},i=e=>{try{a(n.throw(e))}catch(e){r(e)}},a=e=>e.done?o(e.value):Promise.resolve(e.value).then(l,i);a((n=n.apply(e,t)).next())}));const C=y(),A=y();var O=(e,t,n)=>new Promise(((o,r)=>{var l=e=>{try{a(n.next(e))}catch(e){r(e)}},i=e=>{try{a(n.throw(e))}catch(e){r(e)}},a=e=>e.done?o(e.value):Promise.resolve(e.value).then(l,i);a((n=n.apply(e,t)).next())}));const L="storage",x=(e,i,a,s)=>((e,t,i,a,s,c,v,y={},h=[])=>{let O,L,x,T=0,j=0,M=0;g(C,h,(()=>0)),g(A,h,(()=>[]));const[E,I,N,k,B]=((e=1,t)=>1!=e&&t.isMergeable()?[1,t.getMergeableContent,t.getTransactionMergeableChanges,([[e],[t]])=>!d(e)||!d(t),t.setDefaultContent]:2!=e?[0,t.getContent,t.getTransactionChanges,([e,t])=>!d(e)||!d(t),t.setContent]:l("Store type not supported by this Persister"))(v,e),D=t=>{(E&&r(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)},J=e=>P(void 0,null,(function*(){return 2!=T&&(T=1,j++,yield q((()=>P(void 0,null,(function*(){try{const e=yield t();r(e)?D(e):l("Content is not an array "+e)}catch(t){null==c||c(t),e&&B(e)}T=0}))))),G})),z=()=>(L&&(s(L),L=void 0),G),F=e=>P(void 0,null,(function*(){return 1!=T&&(T=2,M++,yield q((()=>P(void 0,null,(function*(){try{yield i(I,e)}catch(e){null==c||c(e)}T=0}))))),G})),V=()=>(o(x,e.delListener),x=void 0,G),q=(...e)=>P(void 0,null,(function*(){return((e,...t)=>{e.push(...t)})(f(A,h),...e),yield P(void 0,null,(function*(){if(!f(C,h)){for(p(C,h,1);!n((e=f(A,h),O=e.shift()));)try{yield O()}catch(e){null==c||c(e)}p(C,h,0)}var e})),G})),G=((e,t)=>{for(var n in t||(t={}))S.call(t,n)&&w(e,n,t[n]);if(b)for(var n of b(t))m.call(t,n)&&w(e,n,t[n]);return e})({load:J,startAutoLoad:e=>P(void 0,null,(function*(){yield z().load(e);try{L=yield a(((e,t)=>P(void 0,null,(function*(){t||e?2!=T&&(T=1,j++,D(null!=t?t:e),T=0):yield J()}))))}catch(e){null==c||c(e)}return G})),stopAutoLoad:z,isAutoLoading:()=>!n(L),save:F,startAutoSave:()=>P(void 0,null,(function*(){return yield V().save(),x=e.addDidFinishTransactionListener((()=>{const e=N();k(e)&&F(e)})),G})),stopAutoSave:V,isAutoSaving:()=>!n(x),schedule:q,getStore:()=>e,destroy:()=>(f(A,h).splice(0,void 0),z().stopAutoSave()),getStats:()=>({loads:j,saves:M})},y);return u(G)})(e,(()=>O(void 0,null,(function*(){return e=a.getItem(i),v(e,((e,t)=>"￼"===t?void 0:t));var e}))),(e=>O(void 0,null,(function*(){return a.setItem(i,(t=e(),c(t,((e,t)=>void 0===t?"￼":t))));var t}))),(e=>{const n=t=>{if(t.storageArea===a&&t.key===i)try{e(v(t.newValue))}catch(t){e()}};return t.addEventListener(L,n),n}),(e=>t.removeEventListener(L,e)),s,3,{getStorageName:()=>i});e.createLocalPersister=(e,t,n)=>x(e,t,localStorage,n),e.createSessionPersister=(e,t,n)=>x(e,t,sessionStorage,n)},"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).TinyBasePersisterBrowser={});
