import{promiseMapLimit as r}from"../../../utils/async.js";import{FileHashStatus as i}from"../../../persistence/file-hashes/types.js";import{OPERATORS as h}from"../../../providers/database/pagination/constants.js";class n{#e;#a;#t;#s;#i;constructor(e){this.#a=e.hashManagerUpdateService,this.#e=e.localHashesCache??void 0,this.#t=e.fileHashesService,this.#s=e.fileType,this.#i={op:h.AND,conditions:[{field:"status",operator:"equal",value:i.OUTDATED},{field:"file_type",operator:"equal",value:this.#s}]}}async getHash(e){if(!this.#e&&!this.#t)return null;if(this.#e)return this.#e.get(e);if(this.#t)return await this.#t.getFileHashByHash(e)}async setHash(e,t){if(!this.#e&&!this.#t)return null;if(this.#e)return this.#e.set(e,t),e;if(this.#t)return await this.#t.upsertFileHash({hash:e,status:t,fileType:this.#s})}async markAllHashesAsOutdated(){if(!(!this.#e&&!this.#t)){if(this.#e)for(const e of this.#e.keys())this.#e.set(e,i.OUTDATED);this.#t&&await this.#t.updateFileHashes(i.OUTDATED,{field:"file_type",operator:"equal",value:this.#s})}}async clearOutdatedHashesAndRelatedRecords(){let e=!0,t;for(;e;){const s=await this.#l(t);await r(s.items,15,async a=>{await this.#a.clearOutdatedRecords(a)}),e=s.page.hasNextPage,t=s.page.endCursor??void 0}await this.#h()}#h=async()=>{if(!this.#e&&!this.#t)return!1;if(this.#e){const e=this.#r();for(const t of e)this.#d(t);return!0}if(this.#t)return await this.#t.deleteFileHashes(this.#i)};#l=async e=>{if(this.#e)return{items:this.#r(),page:{hasNextPage:!1,endCursor:void 0}};if(this.#t){const t=await this.#t.getFileHashes({limit:1e3,filter:this.#i,after:e});return{items:t.items.map(s=>s.hash),page:{hasNextPage:t.page.hasNextPage,endCursor:t.page.endCursor}}}return{items:[],page:{hasNextPage:!1,endCursor:void 0}}};#r=()=>this.#e?Array.from(this.#e.keys()).filter(e=>this.#e?.get(e)==="outdated"):[];#d=e=>{if(!this.#e)return!1;this.#e.delete(e)}}export{n as HashManager};
