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