import{detectSpec as u,getTypes as d,normalizeTypes as w,normalizeVisitors as h,resolveDocument as S,BaseResolver as g,StatsOAS as y,walkDocument as D}from"@redocly/openapi-core";import{logger as m}from"../../../../server/tools/notifiers/logger.js";import{envConfig as I}from"../../../../server/config/env-config.js";import{telemetryTraceStep as O}from"../../../telemetry/helpers/trace-step.js";import{telemetry as v}from"../../../telemetry/index.js";import{computeOas32Stats as b}from"./oas32.js";const t={refs:{metric:"References",total:0,color:"red",items:new Set},externalDocs:{metric:"External Documents",total:0,color:"magenta"},schemas:{metric:"Schemas",total:0,color:"white"},parameters:{metric:"Parameters",total:0,color:"yellow",items:new Set},links:{metric:"Links",total:0,color:"cyan",items:new Set},pathItems:{metric:"Path Items",total:0,color:"green"},webhooks:{metric:"Webhooks",total:0,color:"green"},operations:{metric:"Operations",total:0,color:"yellow"},tags:{metric:"Tags",total:0,color:"white",items:new Set}};async function _(a,o){await O("stats.openapi",async()=>{m.info("OpenAPI collector: start processing documents...");const e=(await a.cache.load(".","load-oas-docs")).data,s=[];for(const r of e)if(!r.isVirtual){const n=await k(r);s.push(n),v.sendStatsOpenapiCollectedMessage([{...n,projectBuildId:I.PROJECT_BUILD_ID||""}])}o||console.table(s),m.info("OpenAPI collector: openapi documents processing completed.")})}async function k(a){const o=a.definition,e=u(o),s=w(d(e)),r=h([{severity:"warn",ruleId:"openapi_stats",visitor:y(t)}],s),n={problems:[],specVersion:e,visitorsData:{}},c={source:{absoluteRef:""},parsed:o},i=s.Root;if(!i)throw new Error("Root type not found in OpenAPI spec types");const f=await S({rootDocument:c,rootType:i,externalRefResolver:new g});D({rootType:i,normalizedVisitors:r,resolvedRefMap:f,document:c,ctx:n});const l={path:a.relativePath,refs:t.refs.total,externalDocs:t.externalDocs.total,schemas:t.schemas.total,parameters:t.parameters.total,links:t.links.total,pathItems:t.pathItems.total,webhooks:t.webhooks.total,operations:t.operations.total,tags:t.tags.total,version:e};if(e==="oas3_2"){const p=b(o);p&&Object.assign(l,p)}return l}export{_ as collectOpenapiDocumentsStatistics};
