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