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