import{CATALOG_ENTITIES_FILES_REGEX as u,ENTITY_SCHEMA_EXCLUDED_FOLDERS as m}from"../../../../constants/plugins/catalog-entities.js";import{FileHashStatus as f,FileType as a}from"../../../../persistence/file-hashes/types.js";import{OPERATORS as d}from"../../../../providers/database/pagination/constants.js";import{promiseMapLimit as h}from"../../../../utils/async/promise-map-limit.js";import{VERSION_NOT_SPECIFIED as p}from"@redocly/theme/core/constants";import{parseEntities as g}from"../../../../web-server/routes/catalog/parsers/entities/parse-entities.js";import{envConfig as T}from"../../../../config/env-config.js";import{extractFileContent as y}from"../../entities/extract-file-content.js";import{resolveEntityVersion as I}from"../../utils/resolve-entity-version.js";import{catalogDataCollector as l}from"../../utils/catalog-data-collector.js";const F=15;class ${#t;#e;#i;#r;#o;#s=new Map;constructor({fileHashManager:t,context:e,catalogEntitiesService:s,catalogConfig:o,shouldCalculateEntities:r}){this.#t=t,this.#e=e,this.#i=s,this.#r=o,this.#o=r}async extract(t){try{if(t&&this.#n(t)){this.#s=await this.#t.getStatusSnapshot(a.ENTITY_DEFINITION),await this.#a(t);return}this.#s=await this.#t.getStatusSnapshot(a.ENTITY_DEFINITION),await this.#t.markAllAsOutdated(a.ENTITY_DEFINITION);const s=this.#e.fs.scan(u).filter(({relativePath:r})=>this.#n(r));s.length&&l.addExtractor("fs");const o=new Set;await h(s,F,async({relativePath:r})=>{o.add(r),await this.#a(r)}),await this.#E(o)}catch(e){this.#e.logger.error("Error extracting entities.",e)}}#n=t=>!!(t.match(u)&&!m.some(e=>t.includes(e)));#a=async t=>{try{const e=await y(t,this.#e);if(!e){this.#e.logger.warn(`Error extracting content from ${t}.`);return}const s=await this.#t.computeFileHash(e),o=await this.#t.getByPath(t),r=this.#s.get(t),i=r!==void 0&&r!==f.UP_TO_DATE;if(!(o?.hash!==s||i||this.#o||T.FORCE_CATALOG_CACHE_REVALIDATE)){l.increaseSkippedFilesCount(),await this.#t.upsert(a.ENTITY_DEFINITION,t,s,f.UP_TO_DATE);return}const c=this.#c(e.entities,t);if(!c)return;const E=await this.#i.getEntityKeysAndVersionsBySourceFile(t);await this.#l(c,t,s,E),l.markSourceFileAsChanged(t),await this.#f(E,t),l.increaseProcessedFilesCount(),await this.#t.upsert(a.ENTITY_DEFINITION,t,s,f.OUTDATED),l.markFileHashPendingConfirmation({fileType:a.ENTITY_DEFINITION,filePath:t,hash:s})}catch(e){this.#e.logger.warn(`Error processing file "${t}". ${e instanceof Error?e.message:String(e)}.`)}};#c=(t,e)=>{try{return g(t,this.#r)}catch(s){return this.#e.logger.warn(`Error validating entities in "${e}". ${s instanceof Error?s.message:String(s)}.`),null}};#l=async(t,e,s,o)=>{for(const r of t)try{const i=I(r.version,e);if(!i.success){this.#e.logger.warn(`Entity "${r.key}" in file "${e}" has conflicting versions: file version "${i.fileVersion}" differs from folder version "${i.folderVersion}". Entity will not be created.`);continue}const n={...r,version:i.version};if(await this.#i.createEntity({entity:n,sourceFile:e,fileHash:s,source:"file"}),r.key){const c=i.version??p;o.delete(`${r.key}:${c}`)}}catch(i){const n=r.key??"unknown";this.#e.logger.warn(`Error processing entity "${n}" from "${e}". ${i instanceof Error?i.message:String(i)}.`)}};#f=async(t,e)=>{if(t.size===0)return;const s=Array.from(t).map(i=>{const[n,c]=i.split(":");return{key:n,version:c}}),o=Array.from(new Set(s.map(({key:i})=>i))),r=s.map(({key:i,version:n})=>({op:d.AND,conditions:[{field:"key",operator:"equal",value:i},{field:"version",operator:"equal",value:n}]}));await this.#i.deleteEntities({op:"AND",conditions:[{field:"key",operator:"in",value:o},{op:d.OR,conditions:r},{field:"source",operator:"equal",value:"file"},{field:"source_file",operator:"equal",value:e}]})};#E=async t=>{const e=await this.#t.getAllOutdated(a.ENTITY_DEFINITION);if(!e||e.length===0)return;const s=e.filter(({filePath:i})=>!t.has(i));if(s.length===0)return;s.forEach(({filePath:i})=>{l.markSourceFileAsRemoved(i)});const o=500,r=s.map(({filePath:i})=>i);for(;;){const i=await this.#i.getEntities({paginationParams:{limit:o,filter:{op:"AND",conditions:[{field:"source_file",operator:"in",value:r},{field:"is_current",operator:"equal",value:!0}]}}});if(!i||i.items.length===0)break;await this.#i.deleteEntities({field:"key",operator:"in",value:i.items.map(({key:n})=>n)})}}}export{$ as FsEntitiesExtractor};
