import{existsSync as t,writeFileSync as a,watch as e}from"fs";import{writeFile as s,readFile as n}from"fs/promises";const r="utf8",o=t=>(a,e,s)=>t(a)?s?.():e(a),i=t=>null==t,c=t=>void 0===t,u=t=>null===t,y=o(i),l=o(c),d=t=>Array.isArray(t),g=t=>t.length,w=t=>{throw Error(t)},p=async(t,a,e)=>{try{return await t()}catch(t){a?.(t)}},h=(t,a)=>t.forEach(a),v=(t,...a)=>t.push(...a),f=t=>t.shift(),C=Object,S=t=>C.getPrototypeOf(t),A=C.keys,b=C.freeze,L=t=>(t=>!i(t)&&y(S(t),t=>t==C.prototype||i(S(t)),()=>!0))(t)&&0==(t=>g(A(t)))(t),M=JSON.stringify,m=JSON.parse,P=t=>c(t)||0==(t=>t?.size??0)(t),O=(t,a)=>t?.forEach(a),E=(t,a)=>t?.delete(a),T=t=>new Map(t),z=(t,a)=>t?.get(a),D=(t,a,e)=>c(e)?(E(t,a),t):t?.set(a,e),F=(t,a,e,s)=>{var n,r;return n=t,r=a,n?.has(r)?s?.(z(t,a)):D(t,a,e()),z(t,a)},J=(t,a,e,s,n=0)=>l((e?F:z)(t,a[n],n>g(a)-2?e:T),r=>{if(n>g(a)-2)return s?.(r)&&D(t,a[n]),r;const o=J(r,a,e,s,n+1);return P(r)&&D(t,a[n]),o}),N=/^\d+$/,j=t=>new Set(d(t)||c(t)?t:[t]),k=T(),x=T(),$=(t,a,e,s,n,r,o,i={},y=0,C=[])=>{let S,A,M,m=0,$=0,q=0;F(k,C,()=>0),F(x,C,()=>[]);const B=T(),[G,H,I,K,Q]=((t=1,a,e)=>1!=t&&a.isMergeable()?[1,a.getMergeableContent,()=>a.getTransactionMergeableChanges(!e),([[t],[a]])=>!L(t)||!L(a),a.setDefaultContent]:2!=t?[0,a.getContent,a.getTransactionChanges,([t,a])=>!L(t)||!L(a),a.setContent]:w("Store type not supported by this Persister"))(o,t,y),[R,U,V]=(()=>{let t;const[a,e]=(()=>{const t=[];let a=0;return[e=>(e?f(t):null)??""+a++,a=>{N.test(a)&&g(t)<1e3&&v(t,a)}]})(),s=T();return[(e,n,r,o=[],i=()=>[])=>{t??=rt;const c=a(1);var u,y;return D(s,c,[e,n,r,o,i]),u=J(n,r??[""],j),y=c,u?.add(y),c},(a,e,...n)=>h(((t,a=[""])=>{const e=[],s=(t,n)=>n==g(a)?v(e,t):u(a[n])?O(t,t=>s(t,n+1)):h([a[n],null],a=>s(z(t,a),n+1));return s(t,0),e})(a,e),a=>O(a,a=>z(s,a)[0](t,...e??[],...n))),t=>l(z(s,t),([,a,n])=>(J(a,n??[""],void 0,a=>(E(a,t),P(a)?1:0)),D(s,t),e(t),n)),a=>l(z(s,a),([a,,e=[],s,n])=>{const r=(...o)=>{const i=g(o);i==g(e)?a(t,...o,...n(o)):u(e[i])?h(s[i]?.(...o)??[],t=>r(...o,t)):r(...o,e[i])};r()})]})(),W=t=>{t!=m&&(m=t,U(B,void 0,m))},X=a=>{(G&&d(a?.[0])?1===a?.[2]?t.applyMergeableChanges:t.setMergeableContent:1===a?.[2]?t.applyChanges:t.setContent)(a)},Y=async t=>(2!=m&&(W(1),$++,await nt(async()=>{await p(async()=>{const e=await a();d(e)?X(e):t?Q(t):w("Content is not an array: "+e)},()=>{t&&Q(t)}),W(0)})),rt),Z=async t=>(_(),await Y(t),await p(async()=>A=await s(async(t,a)=>{a||t?2!=m&&(W(1),$++,X(a??t),W(0)):await Y()}),r),rt),_=async()=>(A&&(await p(()=>n(A),r),A=void 0),rt),tt=async t=>(1!=m&&(W(2),q++,await nt(async()=>{await p(()=>e(H,t),r),W(0)})),rt),at=async()=>(et(),await tt(),M=t.addDidFinishTransactionListener(()=>{const t=I();K(t)&&tt(t)}),rt),et=async()=>(M&&(t.delListener(M),M=void 0),rt),st=async(t=!1)=>{const[a,e]=t?[et,_]:[_,et];return await a(),await e(),rt},nt=async(...t)=>(v(z(x,C),...t),await(async()=>{if(!z(k,C)){for(D(k,C,1);!c(S=f(z(x,C)));)await p(S,r);D(k,C,0)}})(),rt),rt={load:Y,startAutoLoad:Z,stopAutoLoad:_,isAutoLoading:()=>!c(A),save:tt,startAutoSave:at,stopAutoSave:et,isAutoSaving:()=>!c(M),startAutoPersisting:async(t,a=!1)=>{const[e,s]=a?[at,Z]:[Z,at];return await e(t),await s(t),rt},stopAutoPersisting:st,getStatus:()=>m,addStatusListener:t=>R(t,B),delListener:a=>(V(a),t),schedule:nt,getStore:()=>t,destroy:()=>(z(x,C).splice(0,void 0),st()),getStats:()=>({loads:$,saves:q}),...i};return b(rt)},q=(o,i,u)=>$(o,async()=>{return t=await n(i,r),m(t,(t,a)=>"￼"===a?void 0:a);var t},async t=>{return await s(i,(a=t(),M(a,(t,a)=>c(a)?"￼":a)),r);var a},s=>(t(i)||a(i,"",r),e(i,()=>s())),t=>t?.close(),u,3,{getFilePath:()=>i});export{q as createFilePersister};
