const e=globalThis.window,t=e=>null==e,n=(e,n,o)=>t(e)?null==o?void 0:o():n(e),o=e=>Array.isArray(e),r=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),d=JSON.stringify,v=JSON.parse,c=e=>new Map(e),y=(e,t)=>null==e?void 0:e.get(t),g=(e,n,o)=>{return t(o)?(l=n,null==(r=e)||r.delete(l),e):null==e?void 0:e.set(n,o);var r,l},p=(e,t,n,o)=>{var r,l,a;return l=t,null!=(a=null==(r=e)?void 0:r.has(l))&&a||g(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(((o,r)=>{var l=e=>{try{i(n.next(e))}catch(e){r(e)}},a=e=>{try{i(n.throw(e))}catch(e){r(e)}},i=e=>e.done?o(e.value):Promise.resolve(e.value).then(l,a);i((n=n.apply(e,t)).next())}));const C=c(),A=c();var O=(e,t,n)=>new Promise(((o,r)=>{var l=e=>{try{i(n.next(e))}catch(e){r(e)}},a=e=>{try{i(n.throw(e))}catch(e){r(e)}},i=e=>e.done?o(e.value):Promise.resolve(e.value).then(l,a);i((n=n.apply(e,t)).next())}));const P="storage",L=(l,a,i,c)=>((e,l,a,i,d,v,c,h={},O=[])=>{let P,L,M,j=0,x=0,E=0;p(C,O,(()=>0)),p(A,O,(()=>[]));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]:r("Store type not supported by this Persister"))(c,e),J=t=>{(T&&o(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!=j&&(j=1,x++,yield B((()=>m(void 0,null,(function*(){try{const e=yield l();o(e)?J(e):r("Content is not an array "+e)}catch(t){null==v||v(t),e&&D(e)}j=0}))))),G})),F=()=>(L&&(d(L),L=void 0),G),V=e=>m(void 0,null,(function*(){return 1!=j&&(j=2,E++,yield B((()=>m(void 0,null,(function*(){try{yield a(I,e)}catch(e){null==v||v(e)}j=0}))))),G})),q=()=>(n(M,e.delListener),M=void 0,G),B=(...e)=>m(void 0,null,(function*(){return((e,...t)=>{e.push(...t)})(y(A,O),...e),yield m(void 0,null,(function*(){if(!y(C,O)){for(g(C,O,1);!t((e=y(A,O),P=e.shift()));)try{yield P()}catch(e){null==v||v(e)}g(C,O,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!=j&&(j=1,x++,J(null!=t?t:e),j=0):yield z()}))))}catch(e){null==v||v(e)}return G})),stopAutoLoad:F,isAutoLoading:()=>!t(L),save:V,startAutoSave:()=>m(void 0,null,(function*(){return yield q().save(),M=e.addDidFinishTransactionListener((()=>{const e=N();k(e)&&V(e)})),G})),stopAutoSave:q,isAutoSaving:()=>!t(M),schedule:B,getStore:()=>e,destroy:()=>(y(A,O).splice(0,void 0),F().stopAutoSave()),getStats:()=>({loads:x,saves:E})},h);return s(G)})(l,(()=>O(void 0,null,(function*(){return e=i.getItem(a),v(e,((e,t)=>"￼"===t?void 0:t));var e}))),(e=>O(void 0,null,(function*(){return i.setItem(a,(t=e(),d(t,((e,t)=>void 0===t?"￼":t))));var t}))),(t=>{const n=e=>{if(e.storageArea===i&&e.key===a)try{t(v(e.newValue))}catch(e){t()}};return e.addEventListener(P,n),n}),(t=>e.removeEventListener(P,t)),c,3,{getStorageName:()=>a}),M=(e,t,n)=>L(e,t,localStorage,n),j=(e,t,n)=>L(e,t,sessionStorage,n);export{M as createLocalPersister,j as createSessionPersister};
