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