import c from"node:path";import{removeLeadingSlash as u}from"@redocly/theme/core/utils";import{toKebabCase as p}from"../../../../../../utils/string/to-kebab-case.js";import{promiseMapLimit as h}from"../../../../../utils/async.js";import{removeMarkdocTags as f}from"../../../../markdown/markdoc/helpers/remove-markdoc-tags.js";import{BaseApiEntitiesExtractor as y}from"./base.js";const m=15;class I extends y{constructor(t){super("arazzo",t)}mapApiDescriptionToEntity(t){const i=u(t.realRelativePath),a=t.document.info.title,o=p(a),e=o.includes("arazzo")?o:`${o}-arazzo`,r=a?.toLowerCase().includes("arazzo")?a:`${a} - Arazzo`;return{type:this.type,key:e,title:r,summary:t.document.info.description?f(t.document.info.description):null,tags:["arazzo"],metadata:{specType:this.specType,descriptionFile:i}}}async loadApiDescriptions(){return(await this.context.cache.load(".","arazzo-docs")).data}async processApiDescription(t,i){if(!this.#t(t))return;await this.#e(t,i);const a=t.document.sourceDescriptions||[];await h(a,m,async o=>{if(!(o.type!=="openapi"||!o.url))try{const e=this.resolveSourceDescriptionUrl(o.url,t.realRelativePath);if(!e){!o.url.startsWith("http://")&&!o.url.startsWith("https://")&&this.context.logger.warn(`Could not resolve URL to local path: ${o.url}`);return}const r=await this.context.cache.load(e,"load-oas");if(!(r.data&&Array.isArray(r.data)&&r.data.length>0)){this.context.logger.warn(`No OpenAPI definition found at path: ${e}`);return}const n=r.data[0],l=p(n.definition.info.title);await this.catalogEntitiesService.createEntityRelationInLocalDatabase({sourceKey:i,type:"relatesTo",targetKey:l,fileHash:t.hash}),await this.#o(t,o.name,n,i)}catch(e){this.context.logger.warn(`Failed to create relation to OpenAPI source "${o.url}": ${e instanceof Error?e.message:"Unknown error"}`)}})}#t=t=>!!t?.document?.sourceDescriptions&&t.document.sourceDescriptions.length>0;#e=async(t,i)=>{try{const a=t.document.components?.inputs,o=a?Object.entries(a):[],e=[];for(const[r,s]of o)e.push(async()=>{const n=await this.#a({schemaName:r,schema:s,description:t});await this.catalogEntitiesService.createEntityRelationInLocalDatabase({sourceKey:i,type:"uses",targetKey:n,fileHash:t.hash})});if(e.length===0)return;await h(e,m,r=>r())}catch(a){this.context.logger.warn(`Failed to create data schema entities for Arazzo description: ${a instanceof Error?a.message:"Unknown error"}`)}};#a=async({schemaName:t,schema:i,description:a})=>{const o={type:"data-schema",key:p(t),title:t,summary:i.description||i.title||null,tags:["arazzo"],metadata:{specType:this.specType,descriptionFile:a.realRelativePath,schema:"{}"}};return await this.catalogEntitiesService.createEntityInLocalDatabase({entity:o,sourceFile:a.realRelativePath,fileHash:a.hash}),o.key};#o=async(t,i,a,o)=>{const e=t.document.workflows||[];if(e.length===0)return;const r=new Set;for(const s of e)for(const n of s.steps||[]){if(!n.operationId)continue;const l=n.operationId.startsWith(`${i}.`)?n.operationId.substring(`${i}.`.length):n.operationId;l&&r.add(l)}r.size!==0&&await h(Array.from(r),m,async s=>{try{const n=p(s);await this.catalogEntitiesService.createEntityRelationInLocalDatabase({sourceKey:o,type:"uses",targetKey:n,fileHash:t.hash})}catch(n){this.context.logger.warn(`Failed to create relation to operation "${s}": ${n instanceof Error?n.message:"Unknown error"}`)}})};resolveSourceDescriptionUrl(t,i){try{const a=t.trim(),o=a.startsWith("@")?a.slice(1):a,[e]=o.split("#");if(e.startsWith("http://")||e.startsWith("https://"))return null;if(e.startsWith("/"))return u(c.posix.normalize(e));const r=c.posix.dirname(i);return e.startsWith(r+"/")?c.posix.normalize(e):c.posix.normalize(c.posix.join(r,e))}catch{return this.context.logger.warn(`Failed to resolve URL: ${t}`),null}}}export{I as ArazzoEntitiesExtractor};
