/* @alwatr/store-engine v7.1.1 */ "use strict";var h=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var S=(_,e)=>{for(var t in e)h(_,t,{get:e[t],enumerable:!0})},x=(_,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of w(e))!D.call(_,i)&&i!==t&&h(_,i,{get:()=>e[i],enumerable:!(n=p(e,i))||n.enumerable});return _};var b=_=>x(h({},"__esModule",{value:!0}),_);var y={};S(y,{AlwatrStore:()=>f});module.exports=b(y);var u=require("@alwatr/logger"),o=(0,u.definePackage)("@alwatr/store-engine","7.1.1");var g=require("@alwatr/exit-hook"),r=require("@alwatr/node-fs"),s=require("@alwatr/store-helper"),a=require("@alwatr/store-reference"),c=require("@alwatr/store-types"),m=require("@alwatr/wait");o.logModule?.("alwatr-store");var d=class d{constructor(e){this.config__=e;this.cacheReferences__={};var t;o.logMethodArgs?.("new",e),(t=this.config__).defaultChangeDebounce??(t.defaultChangeDebounce=40),this.rootDb__=this.loadRootDb__(),(0,g.exitHook)(this.exitHook__.bind(this))}hasStore(e){let t=(0,s.getStoreId)(e),n=this.rootDb__.hasItem(t);return o.logMethodFull?.("hasStore",t,n),n}newDocument(e,t=null){return o.logMethodArgs?.("newDocument",e),this.newStoreFile_({...e,type:c.StoreFileType.Document},t)}newCollection(e,t=null){return o.logMethodArgs?.("newCollection",e),this.newStoreFile_({...e,type:c.StoreFileType.Collection},t)}newStoreFile_(e,t=null){o.logMethodArgs?.("newStoreFile_",e),e.changeDebounce??(e.changeDebounce=this.config__.defaultChangeDebounce);let n;if(e.type===c.StoreFileType.Document)n=a.DocumentReference.newRefFromData(e,t,this.storeChanged_.bind(this));else if(e.type===c.StoreFileType.Collection)n=a.CollectionReference.newRefFromData(e,t,this.storeChanged_.bind(this));else throw o.accident("newStoreFile_","store_file_type_not_supported",e),new Error("store_file_type_not_supported",{cause:e});if(this.rootDb__.hasItem(n.id))throw o.accident("newStoreFile_","store_file_already_defined",e),new Error("store_file_already_defined",{cause:e});this.rootDb__.addItem(n.id,e),this.cacheReferences__[n.id]=n,this.storeChanged_(n)}async openDocument(e){let t=(0,s.getStoreId)(e);if(o.logMethodArgs?.("openDocument",t),Object.hasOwn(this.cacheReferences__,t)){if(!(this.cacheReferences__[t]instanceof a.DocumentReference))throw o.accident("openDocument","document_wrong_type",t),new Error("document_wrong_type",{cause:t});return this.cacheReferences__[t]}if(!this.rootDb__.hasItem(t))throw o.accident("openDocument","document_not_found",t),new Error("document_not_found",{cause:t});let n=this.rootDb__.getItemData(t);if(n.type!=c.StoreFileType.Document)throw o.accident("openDocument","document_wrong_type",t),new Error("document_wrong_type",{cause:t});let i=await this.readContext__(n),l=a.DocumentReference.newRefFromContext(i,this.storeChanged_.bind(this));return this.cacheReferences__[t]=l,l}async openCollection(e){let t=(0,s.getStoreId)(e);if(o.logMethodArgs?.("openCollection",t),Object.hasOwn(this.cacheReferences__,t)){if(!(this.cacheReferences__[t]instanceof a.CollectionReference))throw o.accident("openCollection","collection_wrong_type",t),new Error("collection_wrong_type",{cause:t});return this.cacheReferences__[t]}if(!this.rootDb__.hasItem(t))throw o.accident("openCollection","collection_not_found",t),new Error("collection_not_found",{cause:t});let n=this.rootDb__.getItemData(t);if(n.type!=c.StoreFileType.Collection)throw o.accident("openCollection","collection_wrong_type",t),new Error("collection_not_found",{cause:t});let i=await this.readContext__(n),l=a.CollectionReference.newRefFromContext(i,this.storeChanged_.bind(this));return this.cacheReferences__[t]=l,l}unloadStore(e){let t=(0,s.getStoreId)(e);o.logMethodArgs?.("unloadStore",t);let n=this.cacheReferences__[t];n!==void 0&&(n.hasUnprocessedChanges_===!0&&(n.updateDelayed_=!1,this.storeChanged_(n)),delete this.cacheReferences__[t])}async removeStore(e){let t=(0,s.getStoreId)(e);if(o.logMethodArgs?.("removeStore",t),!this.rootDb__.hasItem(t))throw o.accident("removeStore","document_not_found",t),new Error("document_not_found",{cause:t});let n=this.cacheReferences__[t];n!==void 0&&(n.freeze=!0,n.updateDelayed_=!1,n.hasUnprocessedChanges_=!1,delete this.cacheReferences__[t]);let i=(0,s.getStorePath)(this.rootDb__.getItemData(t));this.rootDb__.removeItem(t),await(0,m.waitForTimeout)(0);try{await(0,r.unlink)((0,r.resolve)(this.config__.rootPath,i))}catch(l){o.error("removeStore","remove_file_failed",l,{id:e,path:i})}}async saveAll(){o.logMethod?.("saveAll");for(let e of Object.values(this.cacheReferences__))e.hasUnprocessedChanges_===!0&&e.freeze!==!0&&(e.updateDelayed_=!1,await this.storeChanged_(e))}async readContext__(e){typeof e!="string"&&(e=(0,s.getStorePath)(e)),o.logMethodArgs?.("readContext__",e),o.time?.(`readContext__time(${e})`);let t=await(0,r.readJson)((0,r.resolve)(this.config__.rootPath,e));return o.timeEnd?.(`readContext__time(${e})`),t}writeContext__(e,t){return typeof e!="string"&&(e=(0,s.getStorePath)(e)),o.logMethodArgs?.("writeContext__",e),(0,r.writeJson)((0,r.resolve)(this.config__.rootPath,e),t)}async storeChanged_(e){o.logMethodArgs?.("storeChanged__",e.id);let t=e.getStoreMeta().rev;try{await this.writeContext__(e.path,e.getFullContext_()),t===e.getStoreMeta().rev&&(e.hasUnprocessedChanges_=!1)}catch(n){o.error("storeChanged__","write_context_failed",{id:e.id,error:n})}}loadRootDb__(){o.logMethod?.("loadRootDb__");let e=(0,r.resolve)(this.config__.rootPath,(0,s.getStorePath)(d.rootDbStat__));if(!(0,r.existsSync)(e)){if(this.config__.errorWhenNotInitialized===!0)throw new Error("store_not_found",{cause:"Store not initialized"});return o.banner("Initialize new alwatr-store"),a.CollectionReference.newRefFromData(d.rootDbStat__,null,this.storeChanged_.bind(this))}let t=(0,r.readJson)(e,!0);return a.CollectionReference.newRefFromContext(t,this.storeChanged_.bind(this),"root-db")}exitHook__(){o.logMethod?.("exitHook__");for(let e of Object.values(this.cacheReferences__))o.logProperty?.(`StoreFile.${e.id}.hasUnprocessedChanges`,e.hasUnprocessedChanges_),e.hasUnprocessedChanges_===!0&&e.freeze!==!0&&(o.incident?.("exitHook__","rescue_unsaved_context",{id:e.id}),(0,r.writeJson)((0,r.resolve)(this.config__.rootPath,e.path),e.getFullContext_(),!0),e.hasUnprocessedChanges_=!1)}getStoreList(){return o.logMethod?.("getStoreList"),this.rootDb__.values()}};d.version="7.1.1",d.rootDbStat__={name:".store",region:c.Region.Secret,type:c.StoreFileType.Collection,extension:c.StoreFileExtension.Json,changeDebounce:40};var f=d;0&&(module.exports={AlwatrStore}); //# sourceMappingURL=main.cjs.map