import u from"path";import{ASYNC_API_DOCS_TEMPLATE_ID as k}from"../../../constants/index.js";import{combineUrls as A}from"@redocly/theme/core/utils";import{PUBLIC_API_DEFINITIONS_FOLDER as y}from"../../constants/common.js";import{documentToNavItems as w}from"./utils/document-to-nav-items.js";import{navToApiItems as _}from"./utils/nav-to-api-items.js";import{findFirstBinding as E}from"./utils/find-first-binding.js";import{getTemplatePath as C}from"./get-template-path.js";import{storeDefinitionBundles as T}from"./store-definition-bundles.js";import{asyncapiDocLoader as b}from"./asyncapi-doc-loader.js";import{logger as O}from"../../utils/index.js";const R="asyncapi-docs-";async function q(){return{id:"asyncapi",requiredEntitlements:["asyncapi"],loaders:{"asyncapi-doc":b},processContent:async(e,a)=>{if(process.env.REDOCLY_ASYNCAPI_DOCS_ENABLED!=="true")return;if((await a.getConfig()).plugins?.some(c=>c.startsWith("@redocly/portal-plugin-async-api/"))){O.warn("The plugin '@redocly/portal-plugin-async-api' is deprecated. Please remove it from your config to use built-in AsyncAPI docs.");return}const g=e.createTemplate(k,C("./template/AsyncApiDocs.js"));for(const c of await a.fs.scan(/(\.ya?ml|\.json)$/))if(!await a.isPathIgnored(c.relativePath))try{const{data:s,compoundHash:S}=await a.cache.load(c.realRelativePath,"asyncapi-doc");if(!s?.length)continue;T(s,e.outdir,S);for(const t of s){const m=`${R}${t.relativePath}`,h=t.relativePath.split(".")[0],P=E(t.document),f=w({asyncApiDoc:t.document,channelToOperations:t.channelToOperations,protocol:P||""}),I=[{url:A(y,`${D(t.relativePath,".json")}?download`)},{url:A(y,`${D(t.relativePath,".yaml")}?download`)}];await e.createSharedData(m,{document:t.document,apiItems:_(f,h),routingBasePath:h,protocol:P,downloadUrls:I}),f.forEach(i=>{const n={fsPath:t.relativePath,slugSuffix:`/${i.link}`,templateId:g,sharedData:[{key:"AsyncApiDefinition",id:m}],getStaticData:d(a.withPathPrefix,i.label)};e.addRoute(n),i.items&&i.items.forEach(o=>{e.addRoute({...n,slugSuffix:`/${o.link}`,getStaticData:d(a.withPathPrefix,o.label)}),o.items&&o.items.forEach(r=>{e.addRoute({...n,slugSuffix:`/${r.link}`,getStaticData:d(a.withPathPrefix,o.label)})})})}),e.addRoute({fsPath:t.customOutputRelativeFile||t.relativePath,templateId:g,hasClientRoutes:!0,getSidebar:i=>[{type:"link",label:t.document.info?.title??"AsyncAPI Overview",routeSlug:i.slug,link:i.slug},...f.map(n=>({...n,...n.items&&{items:n.items.map(o=>({...o,...o.items&&{items:o.items.map(r=>({...r,routeSlug:i.slug+"/"+r.link,link:i.slug+"/"+r.link}))},routeSlug:i.slug+"/"+o.link,link:i.slug+"/"+o.link}))},routeSlug:i.slug+"/"+n.link,link:i.slug+"/"+n.link}))],getStaticData:d(a.withPathPrefix,t.document.info?.title??"AsyncAPI Docs"),metadata:{type:"asyncapi",title:t.document.info?.title??"AsyncAPI Docs",description:t.document.info?.description??"",...t.document.info?.["x-metadata"]??{}},sharedData:[{key:"AsyncApiDefinition",id:m}]})}}catch(s){console.error(s)}}}}function d(e,a){return async function(l,p){return{props:{settings:{baseUrlPath:e(l.slug)},disableAutoScroll:!0,seo:{title:a}}}}}function D(e,a){const l=u.posix.dirname(e),p=u.posix.basename(e,u.posix.extname(e))+a;return u.posix.join(l,p)}export{q as asyncAPIDocsPlugin};
