"use strict";var t=require("fs"),e=require("fs/promises");const a="utf8",s=t=>null==t,r=(t,e,a)=>s(t)?a?.():e(t),n=t=>Array.isArray(t),i=t=>{throw Error(t)},o=Object,c=t=>o.getPrototypeOf(t),y=o.keys,l=o.freeze,g=t=>(t=>!s(t)&&r(c(t),(t=>t==o.prototype||s(c(t))),(()=>!0)))(t)&&0==(t=>y(t).length)(t),u=JSON.stringify,d=JSON.parse,p=t=>new Map(t),h=(t,e)=>t?.get(e),v=(t,e,a)=>{return s(a)?(r=t,n=e,r?.delete(n),t):t?.set(e,a);var r,n},w=(t,e,a,s)=>{var r,n;return r=t,n=e,r?.has(n)||v(t,e,a()),h(t,e)},C=p(),S=p();exports.createFilePersister=(o,c,y)=>((t,e,a,o,c,y,u,d={},p=[])=>{let f,A,b,F=0,M=0,L=0;w(C,p,(()=>0)),w(S,p,(()=>[]));const[O,P,T,q,x]=((t=1,e)=>1!=t&&e.isMergeable()?[1,e.getMergeableContent,e.getTransactionMergeableChanges,([[t],[e]])=>!g(t)||!g(e),e.setDefaultContent]:2!=t?[0,e.getContent,e.getTransactionChanges,([t,e])=>!g(t)||!g(e),e.setContent]:i("Store type not supported by this Persister"))(u,t),D=e=>{(O&&n(e?.[0])?1===e?.[2]?t.applyMergeableChanges:t.setMergeableContent:1===e?.[2]?t.applyChanges:t.setContent)(e)},J=async t=>(2!=F&&(F=1,M++,await m((async()=>{try{const t=await e();n(t)?D(t):i("Content is not an array "+t)}catch(e){y?.(e),t&&x(t)}F=0}))),z),N=()=>(A&&(c(A),A=void 0),z),j=async t=>(1!=F&&(F=2,L++,await m((async()=>{try{await a(P,t)}catch(t){y?.(t)}F=0}))),z),k=()=>(r(b,t.delListener),b=void 0,z),m=async(...t)=>(((t,...e)=>{t.push(...e)})(h(S,p),...t),await(async()=>{if(!h(C,p)){for(v(C,p,1);!s((t=h(S,p),f=t.shift()));)try{await f()}catch(t){y?.(t)}v(C,p,0)}var t})(),z),z={load:J,startAutoLoad:async t=>{await N().load(t);try{A=await o((async(t,e)=>{e||t?2!=F&&(F=1,M++,D(e??t),F=0):await J()}))}catch(t){y?.(t)}return z},stopAutoLoad:N,isAutoLoading:()=>!s(A),save:j,startAutoSave:async()=>(await k().save(),b=t.addDidFinishTransactionListener((()=>{const t=T();q(t)&&j(t)})),z),stopAutoSave:k,isAutoSaving:()=>!s(b),schedule:m,getStore:()=>t,destroy:()=>(h(S,p).splice(0,void 0),N().stopAutoSave()),getStats:()=>({loads:M,saves:L}),...d};return l(z)})(o,(async()=>{return t=await e.readFile(c,a),d(t,((t,e)=>"￼"===e?void 0:e));var t}),(async t=>{return await e.writeFile(c,(s=t(),u(s,((t,e)=>void 0===e?"￼":e))),a);var s}),(e=>(t.existsSync(c)||t.writeFileSync(c,"",a),t.watch(c,(()=>e())))),(t=>t?.close()),y,3,{getFilePath:()=>c});
