import h from"path";import{simplifyAstStructure as C}from"@redocly/openapi-docs/lib/utils/simplifyAstStructure.js";import{buildMenuItems as E}from"@redocly/asyncapi-docs/lib/utils/build-menu-items.js";import{findFirstBinding as $}from"@redocly/asyncapi-docs/lib/utils/find-first-binding.js";import{ASYNC_API_DOCS_TEMPLATE_ID as w}from"../../../constants/common.js";import{combineUrls as g}from"@redocly/theme/core/utils";import{PUBLIC_API_DEFINITIONS_FOLDER as v}from"../../constants/common.js";import{slash as x}from"../../../utils/path/slash.js";import{logger as T}from"../../tools/notifiers/logger.js";import{getTemplatePath as b}from"./get-template-path.js";import{storeDefinitionBundles as j}from"./store-definition-bundles.js";import{asyncapiDocLoader as F,asyncapiDocsLoader as O}from"./asyncapi-doc-loader.js";import{searchResolver as N}from"./search/search-resolver.js";import{getAiDocumentsStore as L}from"./search/get-ai-search-documents.js";import{telemetryTraceStep as G}from"../../../cli/telemetry/helpers/trace-step.js";const R="asyncapi-docs-";async function et(n){let l=[];return{id:"asyncapi",requiredEntitlements:["asyncapi"],loaders:{"asyncapi-doc":F,"asyncapi-docs":O},processContent:async(e,a)=>{await G("build.plugin.asyncapi_docs",async()=>{if((await a.getConfig()).plugins?.some(d=>d.startsWith("@redocly/portal-plugin-async-api/"))){T.warn("The plugin '@redocly/portal-plugin-async-api' is deprecated. Please remove it from your config to use built-in AsyncAPI docs.");return}const y=e.createTemplate(w,b("./template/AsyncApiDocs.js")),D=e.registerServerPropsGetter(w,b("./get-server-props.js"));for(const d of await a.fs.scan(/(\.ya?ml|\.json)$/))if(!await a.isPathIgnored(d.relativePath))try{const{data:r,compoundHash:S}=await a.cache.load(d.realRelativePath,"asyncapi-doc");if(!r?.length)continue;l=r.map(({markdocChunks:t,relativePath:c,isVirtual:p,customOutputRelativeFile:u,realRelativePath:f})=>({chunks:t,relativePath:c,realRelativePath:f,isVirtual:u!=null||d.isVirtual||p})),j(r,e.outdir,S);for(const t of r){const c=`${R}${t.relativePath}`,p=$(t.document),{navItems:u,apiItems:f}=E({asyncApiDoc:t.document,protocol:p||""}),P=[{url:g(v,`${_(t.relativePath,".json")}?download`)},{url:g(v,`${_(t.relativePath,".yaml")}?download`)}];await e.createSharedData(c,{document:t.document,apiItems:f,protocol:p,downloadUrls:P}),u.forEach(o=>{const s={fsPath:t.relativePath,slugSuffix:`/${o.link}`,templateId:y,sharedData:[{key:"AsyncApiDefinition",id:c}],getStaticData:A(a.withPathPrefix,o.label),serverPropsGetterIds:[D]};e.addRoute(s),o.items&&o.items.forEach(i=>{e.addRoute({...s,slugSuffix:`/${i.link}`,getStaticData:A(a.withPathPrefix,i.label)}),i.items&&i.items.forEach(m=>{e.addRoute({...s,slugSuffix:`/${m.link}`,getStaticData:A(a.withPathPrefix,i.label)})})})}),e.addRoute({fsPath:t.customOutputRelativeFile||t.relativePath,templateId:y,hasClientRoutes:!0,getSidebar:o=>{const s=i=>{const m={...i};return i.link&&(m.routeSlug=g(o.slug,i.link),m.link=g(o.slug,i.link)),i.items&&(m.items=i.items.map(s)),m};return[{type:"link",label:t.document.info?.title??"AsyncAPI Overview",routeSlug:o.slug,link:o.slug},...u.map(s)]},getSearchDocuments:N(e,t.document),getStaticData:A(a.withPathPrefix,t.document.info?.title??"AsyncAPI Docs"),getAiDocumentsStore:L({actions:e,document:t.document,metadata:{type:"asyncapi",title:t.document.info?.title??"AsyncAPI Docs",description:t.document.info?.description??"",...t.document.info?.["x-metadata"]??{}}}),metadata:{type:"asyncapi",title:t.document.info?.title??"AsyncAPI Docs",description:t.document.info?.description??"",...t.document.info?.["x-metadata"]??{}},sharedData:[{key:"AsyncApiDefinition",id:c}]})}}catch(r){console.error(r)}})},afterRoutesCreated:async(e,a)=>{for(const{chunks:I,relativePath:k,isVirtual:y,realRelativePath:D}of l)await a.cache.load(k,{loader:async function(){await a.cache.load(D,"asyncapi-doc");for(const{node:r,markdown:S,pointer:t,key:c,relativePath:p}of I){const[u,...f]=t.split("#"),P=f.join("#"),o=x(h.relative(n.contentDir,u)),{ast:s}=await e.parseMarkdoc({content:S,relativePath:o,isVirtual:y,cacheId:o+(P?"#"+P:"")},a,{sharedDataIds:[`${R}${p}`]});r[`x-parsed-md-${c}`]={result:C(s)}}},name:"asyncapi-markdoc-inline-parser"})}}}function A(n,l){return async function(e,a){return{props:{settings:{baseUrlPath:n(e.baseSlug)},disableAutoScroll:!0,seo:{title:l}}}}}function _(n,l){const e=h.posix.dirname(n),a=h.posix.basename(n,h.posix.extname(n))+l;return h.posix.join(e,a)}export{et as asyncAPIDocsPlugin};
