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