import e from"node:fs";import t from"node:path";import{CacheableMemory as s}from"../../@cacheable/memory/dist/index.mjs";import{Hookified as i}from"../../hookified/dist/node/index.mjs";import{parse as r,stringify as c}from"../../flatted/esm/index.mjs";var a=class extends i{_cache=new s;_cacheDir=".cache";_cacheId="cache1";_persistInterval=0;_persistTimer;_changesSinceLastSave=!1;_parse=r;_stringify=c;constructor(e){super(),e&&(this._cache=new s({ttl:e.ttl,useClone:e.useClone,lruSize:e.lruSize,checkInterval:e.expirationInterval})),e?.cacheDir&&(this._cacheDir=e.cacheDir),e?.cacheId&&(this._cacheId=e.cacheId),e?.persistInterval&&(this._persistInterval=e.persistInterval,this.startAutoPersist()),e?.deserialize&&(this._parse=e.deserialize),e?.serialize&&(this._stringify=e.serialize)}get cache(){return this._cache}get cacheDir(){return this._cacheDir}set cacheDir(e){this._cacheDir=e}get cacheId(){return this._cacheId}set cacheId(e){this._cacheId=e}get changesSinceLastSave(){return this._changesSinceLastSave}get persistInterval(){return this._persistInterval}set persistInterval(e){this._persistInterval=e}load(e,s){try{const i=t.resolve(`${s??this._cacheDir}/${e??this._cacheId}`);this.loadFile(i),this.emit("load")}catch(e){this.emit("error",e)}}loadFile(t){if(e.existsSync(t)){const s=e.readFileSync(t,"utf8"),i=this._parse(s);if(Array.isArray(i))for(const e of i)e&&"object"==typeof e&&"key"in e&&(e.expires?this._cache.set(e.key,e.value,{expire:e.expires}):e.timestamp?this._cache.set(e.key,e.value,{expire:e.timestamp}):this._cache.set(e.key,e.value));else for(const e of Object.keys(i)){const t=i[e];t&&"object"==typeof t&&"key"in t?this._cache.set(t.key,t.value,{expire:t.expires}):t&&"object"==typeof t&&t.timestamp?this._cache.set(e,t,{expire:t.timestamp}):this._cache.set(e,t)}this._changesSinceLastSave=!0}}loadFileStream(t,s,i,r){if(e.existsSync(t)){const c=e.statSync(t).size;let a=0,h="";const n=e.createReadStream(t,{encoding:"utf8"});n.on("data",e=>{a+=e.length,h+=e,s(a,c)}),n.on("end",()=>{const e=this._parse(h);for(const t of Object.keys(e))this._cache.set(e[t].key,e[t].value,{expire:e[t].expires});this._changesSinceLastSave=!0,i()}),n.on("error",e=>{this.emit("error",e),r&&r(e)})}else{const e=new Error(`Cache file ${t} does not exist`);this.emit("error",e),r&&r(e)}}all(){const e={},t=[...this._cache.items];for(const s of t)e[s.key]=s.value;return e}get items(){return[...this._cache.items]}get cacheFilePath(){return t.resolve(`${this._cacheDir}/${this._cacheId}`)}get cacheDirPath(){return t.resolve(this._cacheDir)}keys(){return[...this._cache.keys]}setKey(e,t,s){this.set(e,t,s)}set(e,t,s){this._cache.set(e,t,s),this._changesSinceLastSave=!0}removeKey(e){this.delete(e)}delete(e){this._cache.delete(e),this._changesSinceLastSave=!0,this.emit("delete",e)}getKey(e){return this.get(e)}get(e){return this._cache.get(e)}clear(){try{this._cache.clear(),this._changesSinceLastSave=!0,this.save(),this.emit("clear")}catch(e){this.emit("error",e)}}save(t=!1){try{if(this._changesSinceLastSave||t){const t=this.cacheFilePath,s=[...this._cache.items],i=this._stringify(s);e.existsSync(this._cacheDir)||e.mkdirSync(this._cacheDir,{recursive:!0}),e.writeFileSync(t,i),this._changesSinceLastSave=!1,this.emit("save")}}catch(e){this.emit("error",e)}}removeCacheFile(){try{if(e.existsSync(this.cacheFilePath))return e.rmSync(this.cacheFilePath),!0}catch(e){this.emit("error",e)}return!1}destroy(t=!1){try{this._cache.clear(),this.stopAutoPersist(),t?e.rmSync(this.cacheDirPath,{recursive:!0,force:!0}):e.rmSync(this.cacheFilePath,{recursive:!0,force:!0}),this._changesSinceLastSave=!1,this.emit("destroy")}catch(e){this.emit("error",e)}}startAutoPersist(){this._persistInterval>0&&(this._persistTimer&&(clearInterval(this._persistTimer),this._persistTimer=void 0),this._persistTimer=setInterval(()=>{this.save()},this._persistInterval))}stopAutoPersist(){this._persistTimer&&(clearInterval(this._persistTimer),this._persistTimer=void 0)}};function h(e,t){const s=new a(t);return s.loadFile(e),s}
/* v8 ignore next -- @preserve */export{a as FlatCache,h as createFromFile};