var e,t;e=this,t=function(e){"use strict";const t=globalThis.window,a=e=>null==e,s=(e,t,s)=>a(e)?s?.():t(e),r=e=>Array.isArray(e),n=e=>{throw Error(e)},o=Object,i=e=>o.getPrototypeOf(e),c=o.keys,y=o.freeze,d=e=>(e=>!a(e)&&s(i(e),(e=>e==o.prototype||a(i(e))),(()=>!0)))(e)&&0==(e=>c(e).length)(e),g=JSON.stringify,l=JSON.parse,u=e=>new Map(e),p=(e,t)=>e?.get(t),h=(e,t,s)=>{return a(s)?(r=e,n=t,r?.delete(n),e):e?.set(t,s);var r,n},v=(e,t,a,s)=>{var r,n;return r=e,n=t,r?.has(n)||h(e,t,a()),p(e,t)},f=u(),w=u(),S="storage",b=(e,o,i,c)=>((e,t,o,i,c,g,l,u={},S=[])=>{let b,C,A,L=0,T=0,m=0;v(f,S,(()=>0)),v(w,S,(()=>[]));const[M,P,O,x,E]=((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]:n("Store type not supported by this Persister"))(l,e),N=t=>{(M&&r(t?.[0])?1===t?.[2]?e.applyMergeableChanges:e.setMergeableContent:1===t?.[2]?e.applyChanges:e.setContent)(t)},j=async e=>(2!=L&&(L=1,T++,await I((async()=>{try{const e=await t();r(e)?N(e):n("Content is not an array "+e)}catch(t){g?.(t),e&&E(e)}L=0}))),J),k=()=>(C&&(c(C),C=void 0),J),B=async e=>(1!=L&&(L=2,m++,await I((async()=>{try{await o(P,e)}catch(e){g?.(e)}L=0}))),J),D=()=>(s(A,e.delListener),A=void 0,J),I=async(...e)=>(((e,...t)=>{e.push(...t)})(p(w,S),...e),await(async()=>{if(!p(f,S)){for(h(f,S,1);!a((e=p(w,S),b=e.shift()));)try{await b()}catch(e){g?.(e)}h(f,S,0)}var e})(),J),J={load:j,startAutoLoad:async e=>{await k().load(e);try{C=await i((async(e,t)=>{t||e?2!=L&&(L=1,T++,N(t??e),L=0):await j()}))}catch(e){g?.(e)}return J},stopAutoLoad:k,isAutoLoading:()=>!a(C),save:B,startAutoSave:async()=>(await D().save(),A=e.addDidFinishTransactionListener((()=>{const e=O();x(e)&&B(e)})),J),stopAutoSave:D,isAutoSaving:()=>!a(A),schedule:I,getStore:()=>e,destroy:()=>(p(w,S).splice(0,void 0),k().stopAutoSave()),getStats:()=>({loads:T,saves:m}),...u};return y(J)})(e,(async()=>{return e=i.getItem(o),l(e,((e,t)=>"￼"===t?void 0:t));var e}),(async e=>{return i.setItem(o,(t=e(),g(t,((e,t)=>void 0===t?"￼":t))));var t}),(e=>{const a=t=>{if(t.storageArea===i&&t.key===o)try{e(l(t.newValue))}catch{e()}};return t.addEventListener(S,a),a}),(e=>t.removeEventListener(S,e)),c,3,{getStorageName:()=>o});e.createLocalPersister=(e,t,a)=>b(e,t,localStorage,a),e.createSessionPersister=(e,t,a)=>b(e,t,sessionStorage,a)},"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={});
