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