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