import{removeLeadingSlash as u}from"@redocly/theme/core/utils";import{toKebabCase as i}from"../../../../../../utils/string/to-kebab-case.js";import{promiseMapLimit as l,promiseMapLimitWithStatus as h}from"../../../../../utils/async.js";import{removeMarkdocTags as f}from"../../../../markdown/markdoc/helpers/remove-markdoc-tags.js";import{BaseApiEntitiesExtractor as p}from"./base.js";import{extractPartsFromComponentsRef as m}from"../../../utils/extract-parts-from-components-ref.js";import{validateEntityRelation as g}from"../../../entities/validate-entity.js";import{extractPartsFromChannelsMessageRef as d}from"../../../utils/extract-parts-from-channels-message-ref.js";const o=15;class b extends p{constructor(e){super("asyncapi",e)}mapApiDescriptionToEntity(e){const t=u(e.realRelativePath),a=e.document.info.title,s=i(a);return{type:this.type,key:s,title:a,summary:f(e.document.info.description),tags:e.document.info.tags?.map(n=>n.name),metadata:{specType:this.specType,descriptionFile:t}}}async loadApiDescriptions(){return(await this.context.cache.load(".","asyncapi-docs")).data}async processApiDescription(e,t){const a=this.#t(e),[s]=await Promise.allSettled([this.#a(e,t),this.#n(a,e,t)]);s.status==="rejected"&&this.context.logger.error("Schema processing failed:",s.reason)}#t=e=>{const t=e.documentWithReferences.operations;return t?Object.entries(t).map(([a,s])=>({operationName:a,operation:s})):[]};#a=async(e,t)=>{const a=e.document.components?.schemas;if(!a)return;const s=Object.entries(a);if(s.length===0)return;const n=await h(s,o,async([r,c])=>{const y=await this.#s({schemaName:r,schema:c,description:e});await this.catalogEntitiesService.createEntityRelationInLocalDatabase({sourceKey:t,type:"uses",targetKey:y,fileHash:e.hash})},this.context.logger);n.count.failed>0&&this.context.logger.warn(`Schema processing completed with ${n.count.failed} failures out of ${s.length} schemas for ${e.realRelativePath}`)};#s=async({schemaName:e,schema:t,description:a})=>{const s=i(e),n="title"in t?t.title??t.description:null,r=JSON.stringify(t),c={type:"data-schema",key:s,title:e,summary:n,tags:[],metadata:{specType:this.specType,schema:r}};return await this.catalogEntitiesService.createEntityInLocalDatabase({entity:c,sourceFile:a.realRelativePath,fileHash:a.hash}),c.key};#n=async(e,t,a)=>e.length?{failedCount:(await h(e,o,async({operationName:n,operation:r})=>{await this.#r(r,n,t,a)},this.context.logger)).count.failed,totalCount:e.length}:{failedCount:0,totalCount:0};#r=async(e,t,a,s)=>{const n=await this.#c({apiOperationKey:i(t),operation:e,descriptionUniqueKey:s,description:a});await this.#i(t,e,n,a)};#i=async(e,t,a,s)=>{const n={type:"api-operation",key:i(e),title:t.title||e,summary:t.summary??"",tags:t.tags?.map(r=>r.name)??[],metadata:{method:t.action==="send"?"PUBLISH":"SUBSCRIBE",path:t.channel.address??"",payload:t.action==="receive"?a:[],responses:t.action==="send"?a:[]}};return await this.catalogEntitiesService.createEntityInLocalDatabase({entity:n,sourceFile:s.realRelativePath,fileHash:s.hash}),n.key};#c=async({apiOperationKey:e,operation:t,descriptionUniqueKey:a,description:s})=>(await this.catalogEntitiesService.createEntityRelationInLocalDatabase({sourceKey:e,type:"partOf",targetKey:a,fileHash:s.hash}),await this.#o(t,e),!Array.isArray(t.messages)||!t.messages?.length?[]:await this.#l(t.messages,e,s));#o=async(e,t)=>{e["x-catalog-relations"]?.length&&await l(e["x-catalog-relations"],o,async a=>{try{g(a),await this.catalogEntitiesService.createEntityRelationInLocalDatabase({sourceKey:t,type:a.type,targetKey:a.key})}catch(s){this.context.logger.warn(`Error creating entity relation for operation ${t} based on custom property: ${s instanceof Error?s.message:"Unknown error"}`)}})};#l=async(e,t,a)=>e.length===0?[]:(await l(e,o,async n=>await this.#h(n,t,a)??"")).filter(n=>!!n);#h=async(e,t,a)=>"$ref"in e?this.#m(e,t,a):e.payload?this.#u(e,t,a):null;#m=async(e,t,a)=>{const s=e.$ref.startsWith("#/channels/")?this.#y(e.$ref,a):e.$ref;if(!s)return null;const n=m(s);if(!n||!n.componentName||n.componentType!=="messages")return null;const r=a.documentWithReferences.components?.messages[n.componentName];return!r?.payload||!("$ref"in r.payload)||!r.payload.$ref?null:this.#e(r.payload.$ref,t,a)};#y=(e,t)=>{const a=d(e);if(!a||!a.channelName||!a.messageName)return null;const s=t.documentWithReferences.channels?.[a.channelName];if(!s?.messages)return null;const n=s.messages[a.messageName];return!n||!("$ref"in n)||typeof n.$ref!="string"||!n.$ref.startsWith("#/components/messages/")?null:n.$ref};#u=async(e,t,a)=>!e.payload||!("$ref"in e.payload)||!e.payload.$ref?null:this.#e(e.payload.$ref,t,a);#e=async(e,t,a)=>{const s=m(e);if(!s||!s.componentName||s.componentType!=="schemas")return null;const n=i(s.componentName);return await this.catalogEntitiesService.createEntityRelationInLocalDatabase({sourceKey:t,type:"uses",targetKey:n,fileHash:a.hash}),n}}export{b as AsyncApiEntitiesExtractor};
