import{CATALOG_BASE_SLUG as o,ENTITIES_MAP_GLOBAL_DATA_KEY as S,CATALOG_FILTERS_CACHE_NAMESPACE as T}from"../../constants/plugins/catalog-entities.js";import{CatalogEntitiesService as v}from"./database/catalog-entities-service.js";import{CacheService as _}from"../../persistence/cache/services/cache-service.js";import{getTemplatePath as g}from"./utils/get-template-path.js";import{getCompleteCatalogConfig as D}from"./get-complete-catalog-config.js";import{AsyncApiEntitiesExtractor as w}from"./extensions/extractors/api-description/asyncapi-entities-extractor.js";import{OpenApiEntitiesExtractor as y}from"./extensions/extractors/api-description/openapi-entities-extractor.js";import{GraphqlEntitiesExtractor as O}from"./extensions/extractors/api-description/graphql-entities-extractor.js";import{FsEntitiesExtractor as L}from"./extensions/extractors/fs-entities-extractor.js";import{FileHashesService as P}from"../../persistence/file-hashes/services/file-hashes-service.js";import{ArazzoEntitiesExtractor as I}from"./extensions/extractors/api-description/arazzo-entities-extractor.js";import{telemetryTraceStep as b}from"../../../cli/telemetry/helpers/trace-step.js";const G="catalog-entity-template",N="catalog-entity",R=new Map,x=new Map,M=new Map,H=new Map;let c=!0;async function z(){return{id:"CatalogEntities",requiredEntitlements:["catalog"],async processContent(e,t){await b("build.plugin.catalog_entities",async m=>{if(process.env.NEW_CATALOG_ENABLED!=="true")return;const u=await t.getConfig(),i=D(u.entitiesCatalog);if(m?.setAttribute("config",JSON.stringify(i)),!i.show)return;const{logger:n}=t,r=process.env.NODE_ENV==="development"||process.env.REDOCLY_LOCAL_DEV==="true",a=await v.getInstance({baseDbDir:e.serverOutDir,removeExisting:c&&r,runOnlyLocalDatabase:!0,runWithPragmaWalWriteOptimization:!0}),s=r?void 0:await P.getInstance({baseDbDir:e.serverOutDir}),E=[new L({context:t,catalogEntitiesService:a,fileHashesService:s,isDevMode:r,isFirstPluginRun:c,catalogConfig:i}),new y({actions:e,context:t,catalogEntitiesService:a,fileHashesService:s,hashesCache:r?R:void 0}),new w({actions:e,context:t,catalogEntitiesService:a,fileHashesService:s,hashesCache:r?x:void 0}),new O({actions:e,context:t,catalogEntitiesService:a,fileHashesService:s,hashesCache:r?M:void 0}),new I({actions:e,context:t,catalogEntitiesService:a,fileHashesService:s,hashesCache:r?H:void 0})];n.info("Starting entities extractors...");const f=n.startTiming();await a.transaction(async()=>{await Promise.all(E.map(async p=>p.extract()))});const h=a.getEntitySources();e.setGlobalData({[S]:h}),await(await _.getInstance({baseDbDir:e.serverOutDir})).deleteByNamespace(T),n.infoTime(f,"Entities extractors finished");const d=e.registerServerPropsGetter(N,g("../get-server-props.js")),C=e.createTemplate(G,g("../template/index.js"));e.addRoute({duplicateInAllLocales:!0,slug:o,fsPath:"",templateId:C,excludeFromSidebar:!0,hasClientRoutes:!0,serverPropsGetterIds:[d],getNavText:()=>Promise.resolve("Catalog"),getStaticData:async()=>({props:{catalogConfig:i}})});const[l]=Object.entries(i.catalogs??{}).find(([p,A])=>!A?.hide)||[];l&&e.addRedirect(o,{type:302,to:`${o}/${l}`}),n.info("Catalog Entities plugin finished"),c=!1})}}}var X=z;export{z as catalogEntitiesPlugin,X as default};
