"use strict";const e=globalThis.window,t=e=>null==e,n=(e,n,r)=>t(e)?null==r?void 0:r():n(e),r=e=>Array.isArray(e),o=e=>{throw Error(e)},l=Object,a=e=>l.getPrototypeOf(e),i=l.keys,s=l.freeze,u=e=>(e=>!t(e)&&n(a(e),(e=>e==l.prototype||t(a(e))),(()=>!0)))(e)&&0==(e=>i(e).length)(e),c=JSON.stringify,d=JSON.parse,v=e=>new Map(e),y=(e,t)=>null==e?void 0:e.get(t),p=(e,n,r)=>{return t(r)?(l=n,null==(o=e)||o.delete(l),e):null==e?void 0:e.set(n,r);var o,l},g=(e,t,n,r)=>{var o,l,a;return l=t,null!=(a=null==(o=e)?void 0:o.has(l))&&a||p(e,t,n()),y(e,t)};var h=Object.defineProperty,f=Object.getOwnPropertySymbols,b=Object.prototype.hasOwnProperty,S=Object.prototype.propertyIsEnumerable,w=(e,t,n)=>t in e?h(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,m=(e,t,n)=>new Promise(((r,o)=>{var l=e=>{try{i(n.next(e))}catch(e){o(e)}},a=e=>{try{i(n.throw(e))}catch(e){o(e)}},i=e=>e.done?r(e.value):Promise.resolve(e.value).then(l,a);i((n=n.apply(e,t)).next())}));const C=v(),P=v();var A=(e,t,n)=>new Promise(((r,o)=>{var l=e=>{try{i(n.next(e))}catch(e){o(e)}},a=e=>{try{i(n.throw(e))}catch(e){o(e)}},i=e=>e.done?r(e.value):Promise.resolve(e.value).then(l,a);i((n=n.apply(e,t)).next())}));const O="storage",L=(l,a,i,v)=>((e,l,a,i,c,d,v,h={},A=[])=>{let O,L,x,M=0,j=0,E=0;g(C,A,(()=>0)),g(P,A,(()=>[]));const[T,I,N,k,D]=((e=1,t)=>1!=e&&t.isMergeable()?[1,t.getMergeableContent,t.getTransactionMergeableChanges,([[e],[t]])=>!u(e)||!u(t),t.setDefaultContent]:2!=e?[0,t.getContent,t.getTransactionChanges,([e,t])=>!u(e)||!u(t),t.setContent]:o("Store type not supported by this Persister"))(v,e),J=t=>{(T&&r(null==t?void 0:t[0])?1===(null==t?void 0:t[2])?e.applyMergeableChanges:e.setMergeableContent:1===(null==t?void 0:t[2])?e.applyChanges:e.setContent)(t)},z=e=>m(void 0,null,(function*(){return 2!=M&&(M=1,j++,yield B((()=>m(void 0,null,(function*(){try{const e=yield l();r(e)?J(e):o("Content is not an array "+e)}catch(t){null==d||d(t),e&&D(e)}M=0}))))),G})),F=()=>(L&&(c(L),L=void 0),G),V=e=>m(void 0,null,(function*(){return 1!=M&&(M=2,E++,yield B((()=>m(void 0,null,(function*(){try{yield a(I,e)}catch(e){null==d||d(e)}M=0}))))),G})),q=()=>(n(x,e.delListener),x=void 0,G),B=(...e)=>m(void 0,null,(function*(){return((e,...t)=>{e.push(...t)})(y(P,A),...e),yield m(void 0,null,(function*(){if(!y(C,A)){for(p(C,A,1);!t((e=y(P,A),O=e.shift()));)try{yield O()}catch(e){null==d||d(e)}p(C,A,0)}var e})),G})),G=((e,t)=>{for(var n in t||(t={}))b.call(t,n)&&w(e,n,t[n]);if(f)for(var n of f(t))S.call(t,n)&&w(e,n,t[n]);return e})({load:z,startAutoLoad:e=>m(void 0,null,(function*(){yield F().load(e);try{L=yield i(((e,t)=>m(void 0,null,(function*(){t||e?2!=M&&(M=1,j++,J(null!=t?t:e),M=0):yield z()}))))}catch(e){null==d||d(e)}return G})),stopAutoLoad:F,isAutoLoading:()=>!t(L),save:V,startAutoSave:()=>m(void 0,null,(function*(){return yield q().save(),x=e.addDidFinishTransactionListener((()=>{const e=N();k(e)&&V(e)})),G})),stopAutoSave:q,isAutoSaving:()=>!t(x),schedule:B,getStore:()=>e,destroy:()=>(y(P,A).splice(0,void 0),F().stopAutoSave()),getStats:()=>({loads:j,saves:E})},h);return s(G)})(l,(()=>A(void 0,null,(function*(){return e=i.getItem(a),d(e,((e,t)=>"￼"===t?void 0:t));var e}))),(e=>A(void 0,null,(function*(){return i.setItem(a,(t=e(),c(t,((e,t)=>void 0===t?"￼":t))));var t}))),(t=>{const n=e=>{if(e.storageArea===i&&e.key===a)try{t(d(e.newValue))}catch(e){t()}};return e.addEventListener(O,n),n}),(t=>e.removeEventListener(O,t)),v,3,{getStorageName:()=>a});exports.createLocalPersister=(e,t,n)=>L(e,t,localStorage,n),exports.createSessionPersister=(e,t,n)=>L(e,t,sessionStorage,n);
