"use strict";var e=require("fs"),t=require("fs/promises");const n="utf8",r=e=>null==e,l=(e,t,n)=>r(e)?null==n?void 0:n():t(e),o=e=>Array.isArray(e),i=e=>{throw Error(e)},a=Object,u=e=>a.getPrototypeOf(e),s=a.keys,d=a.freeze,c=e=>(e=>!r(e)&&l(u(e),(e=>e==a.prototype||r(u(e))),(()=>!0)))(e)&&0==(e=>s(e).length)(e),v=JSON.stringify,y=JSON.parse,p=e=>new Map(e),f=(e,t)=>null==e?void 0:e.get(t),h=(e,t,n)=>{return r(n)?(o=t,null==(l=e)||l.delete(o),e):null==e?void 0:e.set(t,n);var l,o},g=(e,t,n,r)=>{var l,o,i;return o=t,null!=(i=null==(l=e)?void 0:l.has(o))&&i||h(e,t,n()),f(e,t)};var b=Object.defineProperty,w=Object.getOwnPropertySymbols,S=Object.prototype.hasOwnProperty,C=Object.prototype.propertyIsEnumerable,P=(e,t,n)=>t in e?b(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,O=(e,t,n)=>new Promise(((r,l)=>{var o=e=>{try{a(n.next(e))}catch(e){l(e)}},i=e=>{try{a(n.throw(e))}catch(e){l(e)}},a=e=>e.done?r(e.value):Promise.resolve(e.value).then(o,i);a((n=n.apply(e,t)).next())}));const A=p(),m=p();var x=(e,t,n)=>new Promise(((r,l)=>{var o=e=>{try{a(n.next(e))}catch(e){l(e)}},i=e=>{try{a(n.throw(e))}catch(e){l(e)}},a=e=>e.done?r(e.value):Promise.resolve(e.value).then(o,i);a((n=n.apply(e,t)).next())}));exports.createFilePersister=(a,u,s)=>((e,t,n,a,u,s,v,y={},p=[])=>{let b,x,F,M=0,j=0,L=0;g(A,p,(()=>0)),g(m,p,(()=>[]));const[T,q,D,E,J]=((e=1,t)=>1!=e&&t.isMergeable()?[1,t.getMergeableContent,t.getTransactionMergeableChanges,([[e],[t]])=>!c(e)||!c(t),t.setDefaultContent]:2!=e?[0,t.getContent,t.getTransactionChanges,([e,t])=>!c(e)||!c(t),t.setContent]:i("Store type not supported by this Persister"))(v,e),N=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)},k=e=>O(void 0,null,(function*(){return 2!=M&&(M=1,j++,yield G((()=>O(void 0,null,(function*(){try{const e=yield t();o(e)?N(e):i("Content is not an array "+e)}catch(t){null==s||s(t),e&&J(e)}M=0}))))),H})),z=()=>(x&&(u(x),x=void 0),H),I=e=>O(void 0,null,(function*(){return 1!=M&&(M=2,L++,yield G((()=>O(void 0,null,(function*(){try{yield n(q,e)}catch(e){null==s||s(e)}M=0}))))),H})),B=()=>(l(F,e.delListener),F=void 0,H),G=(...e)=>O(void 0,null,(function*(){return((e,...t)=>{e.push(...t)})(f(m,p),...e),yield O(void 0,null,(function*(){if(!f(A,p)){for(h(A,p,1);!r((e=f(m,p),b=e.shift()));)try{yield b()}catch(e){null==s||s(e)}h(A,p,0)}var e})),H})),H=((e,t)=>{for(var n in t||(t={}))S.call(t,n)&&P(e,n,t[n]);if(w)for(var n of w(t))C.call(t,n)&&P(e,n,t[n]);return e})({load:k,startAutoLoad:e=>O(void 0,null,(function*(){yield z().load(e);try{x=yield a(((e,t)=>O(void 0,null,(function*(){t||e?2!=M&&(M=1,j++,N(null!=t?t:e),M=0):yield k()}))))}catch(e){null==s||s(e)}return H})),stopAutoLoad:z,isAutoLoading:()=>!r(x),save:I,startAutoSave:()=>O(void 0,null,(function*(){return yield B().save(),F=e.addDidFinishTransactionListener((()=>{const e=D();E(e)&&I(e)})),H})),stopAutoSave:B,isAutoSaving:()=>!r(F),schedule:G,getStore:()=>e,destroy:()=>(f(m,p).splice(0,void 0),z().stopAutoSave()),getStats:()=>({loads:j,saves:L})},y);return d(H)})(a,(()=>x(void 0,null,(function*(){return e=yield t.readFile(u,n),y(e,((e,t)=>"￼"===t?void 0:t));var e}))),(e=>x(void 0,null,(function*(){return yield t.writeFile(u,(r=e(),v(r,((e,t)=>void 0===t?"￼":t))),n);var r}))),(t=>(e.existsSync(u)||e.writeFileSync(u,"",n),e.watch(u,(()=>t())))),(e=>null==e?void 0:e.close()),s,3,{getFilePath:()=>u});
