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