const t=globalThis.window,e=t=>null==t,a=(t,a,n)=>e(t)?n?.():a(t),n=t=>Array.isArray(t),s=t=>t.length,r=t=>{throw Error(t)},o=async(t,e,a)=>{try{return await t()}catch(t){e?.(t)}},i=(t,e)=>t.forEach(e),c=(t,...e)=>t.push(...e),g=t=>t.shift(),l=Object,u=t=>l.getPrototypeOf(t),y=l.keys,d=l.freeze,w=t=>(t=>!e(t)&&a(u(t),t=>t==l.prototype||e(u(t)),()=>!0))(t)&&0==(t=>s(y(t)))(t),p=JSON.stringify,v=JSON.parse,h=t=>v(t,(t,e)=>"￼"===e?void 0:e),S=t=>e(t)||0==(t=>t?.size??0)(t),A=(t,e)=>t?.forEach(e),C=(t,e)=>t?.delete(e),f=t=>new Map(t),L=(t,e)=>t?.get(e),b=(t,a,n)=>e(n)?(C(t,a),t):t?.set(a,n),M=(t,e,a,n)=>{var s,r;return s=t,r=e,s?.has(r)?n?.(L(t,e)):b(t,e,a()),L(t,e)},E=(t,e,n,r,o=0)=>a((n?M:L)(t,e[o],o>s(e)-2?n:f),a=>{if(o>s(e)-2)return r?.(a)&&b(t,e[o]),a;const i=E(a,e,n,r,o+1);return S(a)&&b(t,e[o]),i}),m=/^\d+$/,O=t=>new Set(n(t)||e(t)?t:[t]),P=f(),T=f(),N=(t,l,u,y,p,v,h,N={},k=0,z=[])=>{let D,I,J,j=0,x=0,F=0;M(P,z,()=>0),M(T,z,()=>[]);const V=f(),[$,q,B,G,H]=((t=1,e,a)=>1!=t&&e.isMergeable()?[1,e.getMergeableContent,()=>e.getTransactionMergeableChanges(!a),([[t],[e]])=>!w(t)||!w(e),e.setDefaultContent]:2!=t?[0,e.getContent,e.getTransactionChanges,([t,e])=>!w(t)||!w(e),e.setContent]:r("Store type not supported by this Persister"))(h,t,k),[K,Q,R]=(()=>{let t;const[n,r]=(()=>{const t=[];let e=0;return[a=>(a?g(t):null)??""+e++,e=>{m.test(e)&&s(t)<1e3&&c(t,e)}]})(),o=f();return[(e,a,s,r=[],i=()=>[])=>{t??=st;const c=n(1);var g,l;return b(o,c,[e,a,s,r,i]),g=E(a,s??[""],O),l=c,g?.add(l),c},(e,a,...n)=>i(((t,e=[""])=>{const a=[],n=(t,r)=>r==s(e)?c(a,t):null===e[r]?A(t,t=>n(t,r+1)):i([e[r],null],e=>n(L(t,e),r+1));return n(t,0),a})(e,a),e=>A(e,e=>L(o,e)[0](t,...a??[],...n))),t=>a(L(o,t),([,e,a])=>(E(e,a??[""],void 0,e=>(C(e,t),S(e)?1:0)),b(o,t),r(t),a)),n=>a(L(o,n),([a,,n=[],r,o])=>{const c=(...g)=>{const l=s(g);l==s(n)?a(t,...g,...o(g)):e(n[l])?i(r[l]?.(...g)??[],t=>c(...g,t)):c(...g,n[l])};c()})]})(),U=t=>{t!=j&&(j=t,Q(V,void 0,j))},W=e=>{($&&n(e?.[0])?1===e?.[2]?t.applyMergeableChanges:t.setMergeableContent:1===e?.[2]?t.applyChanges:t.setContent)(e)},X=async t=>(2!=j&&(U(1),x++,await nt(async()=>{await o(async()=>{const e=await l();n(e)?W(e):t?H(t):r("Content is not an array: "+e)},()=>{t&&H(t)}),U(0)})),st),Y=async t=>(Z(),await X(t),await o(async()=>I=await y(async(t,e)=>{e||t?2!=j&&(U(1),x++,W(e??t),U(0)):await X()}),v),st),Z=async()=>(I&&(await o(()=>p(I),v),I=void 0),st),_=async t=>(1!=j&&(U(2),F++,await nt(async()=>{await o(()=>u(q,t),v),U(0)})),st),tt=async()=>(et(),await _(),J=t.addDidFinishTransactionListener(()=>{const t=B();G(t)&&_(t)}),st),et=async()=>(J&&(t.delListener(J),J=void 0),st),at=async(t=!1)=>{const[e,a]=t?[et,Z]:[Z,et];return await e(),await a(),st},nt=async(...t)=>(c(L(T,z),...t),await(async()=>{if(!L(P,z)){for(b(P,z,1);!e(D=g(L(T,z)));)await o(D,v);b(P,z,0)}})(),st),st={load:X,startAutoLoad:Y,stopAutoLoad:Z,isAutoLoading:()=>!e(I),save:_,startAutoSave:tt,stopAutoSave:et,isAutoSaving:()=>!e(J),startAutoPersisting:async(t,e=!1)=>{const[a,n]=e?[tt,Y]:[Y,tt];return await a(t),await n(t),st},stopAutoPersisting:at,getStatus:()=>j,addStatusListener:t=>K(t,V),delListener:e=>(R(e),t),schedule:nt,getStore:()=>t,destroy:()=>(L(T,z).splice(0,void 0),at()),getStats:()=>({loads:x,saves:F}),...N};return d(st)},k="storage",z=(e,a,n,s)=>N(e,async()=>h(n.getItem(a)),async t=>{return n.setItem(a,(e=t(),p(e,(t,e)=>void 0===e?"￼":e)));var e},e=>{const s=t=>{t.storageArea===n&&t.key===a&&o(()=>e(h(t.newValue)),e)};return t.addEventListener(k,s),s},e=>t.removeEventListener(k,e),s,3,{getStorageName:()=>a}),D=(t,e,a)=>z(t,e,localStorage,a),I=(t,e,a)=>z(t,e,sessionStorage,a);export{D as createLocalPersister,I as createSessionPersister};
