import J from"node:path";import{simplifyAstStructure as K}from"@redocly/openapi-docs/lib/utils/simplifyAstStructure.js";import{REDOCLY_TEAMS_RBAC as b}from"@redocly/config";import{OPENAPI_DOCS_TEMPLATE_ID as j,PUBLIC_RBAC_SCOPE_ITEM as Z}from"../../../constants/index.js";import{PUBLIC_API_DEFINITIONS_FOLDER as ee}from"../../constants/common.js";import{searchResolver as te}from"./search/search-resolver.js";import{convertOpenAPIDocs2Sidebar as oe,shouldAddRoute as re}from"./utils.js";import{getTemplatePath as y}from"./get-template-path.js";import{isDevelopMode as G}from"../../utils/is-develop-mode.js";import{OPENAPI_CUSTOM_FIELDS_SERVER_PROPS_GETTER_ID as ae,OPENAPI_SHARED_DATA_PREFIX as N}from"./constants.js";import{storeDefinitionBundles as se}from"./store-definition-bundles.js";import{definitionLoader as ie,definitionsLoader as ne}from"./load-definition.js";import{slash as pe}from"../../../shared/utils.js";import{getAiDocumentsStore as ce}from"./search/get-ai-search-documents.js";import{fromCurrentDir as de}from"../../utils/paths.js";const $="openapi-spec-download";async function Ce(V){let T=[],A={};return{id:"openapi",requiredEntitlements:["openapi"],loaders:{"load-oas-docs":ne,"load-oas":ie},processContent:async(e,i)=>{e.createRequestHandler($,de(import.meta.url,"./spec-download.api.js")),e.addApiRoute({slug:ee+"/*",requestHandlerId:$,httpMethod:"all",[b]:Z,getStaticData:async()=>({props:{}})});const p=e.createTemplate(j,y("./template/OpenAPIDocs.js")),r=e.registerServerPropsGetter(j,y("./get-server-props.js")),P=e.registerServerPropsGetter(ae,y("./get-server-props-custom-fields.js")),c=await i.getConfig(),_=c.rules?.["custom-fields-schema"];A={};const d=await e.loadOpenApiDefinitions(i);T=d.map(({markdocChunks:l,relativePath:u,customOutputRelativeFile:s,isVirtual:a,realRelativePath:m})=>({chunks:l,relativePath:u,realRelativePath:m,isVirtual:s!=null||a})),se(d,e.outdir);const S={};for(const l of d||[]){const{definition:u,config:s,relativePath:a,customOutputRelativeFile:m,contentItems:I,flatItems:f,parser:h,options:M,rawOptions:H,hash:U}=l,L=m||a,o=[],k={},{definition:q}=h||{},{info:n}=q||{},E=n?.["x-metadata"],w=!!s.openapi?.excludeFromSearch||!!s.theme?.openapi?.excludeFromSearch||!!c.openapi?.excludeFromSearch||!!c.theme?.openapi?.excludeFromSearch,x={title:n?.title,description:n?.description,summary:n?.summary,...s.metadata,...E},g={untagged:[],tagged:new Map};for(const t of f){const{id:v,href:D,operationDefinition:C}=t;if(C){const{tags:R}=C;if(R)for(const F of R)g.tagged.has(F)||g.tagged.set(F,[]),g.tagged.get(F)?.push(t);else g.untagged.push(t);G()&&(k[`#${C.pointer}`]=t.href)}if(!re({item:t}))continue;const O=t,X=O.type==="section"&&!!O.infoDefinition,Y=D.split("#")[0]+"/",z=t?.operationDefinition?.[b];o.push({slugSuffix:Y,fsPath:L,httpVerb:t?.httpVerb||"",path:a,templateId:p,[b]:z||s.rbac,getAiDocumentsStore:ce({parser:h,options:M,info:n,tagOperations:g,openapiContentItem:O,metadata:x,relativePath:a,getSearchFacets:e.getSearchFacets,includeInLLMsTxt:X,excludeFromSearch:w}),getStaticData:async R=>({props:{dynamicMarkdocComponents:["openapi"],baseSlug:R.baseSlug,seo:t["x-metadata"]?.seo||{title:t.name,description:t.description},itemId:v,disableAutoScroll:!0}})})}o[0]={...o[0],metadata:{type:"openapi",...x},hasClientRoutes:!0,getSidebar:(t,v)=>{const D=[];return oe({contentItems:I,sidebarItems:D,routeSlug:t.slug,navItem:v}),D},getNavText:()=>n?.title,getSearchDocuments:te(h,M,f,e.getSearchFacets,e.setSearchFacets,w)},E?.apiId&&(S[E.apiId]={slug:o[0]?.slug||""});const Q=o[0];o[0]=o[o.length-1],o[o.length-1]=Q;for(const t of o)e.addRoute({...t,serverPropsGetterIds:_?[r,P]:[r]});const W=G()?a:void 0,B=`${N}${a}`;A[B]={fsPath:L,definition:u,options:H,sourcePath:W,routesMapping:k,hash:U};for(const t of o)e.addRoute({...t,sharedData:[{id:B,key:"openAPIDocsStore"}],serverPropsGetterIds:_?[r,P]:[r]})}e.setGlobalData({apiProducts:S})},afterRoutesCreated:async(e,i)=>{for(const{chunks:p,relativePath:r,isVirtual:P,realRelativePath:c}of T)await i.cache.load(r,{loader:async function(){await i.cache.load(c,"load-oas");for(const{node:d,markdown:S,pointer:l,key:u,relativePath:s}of p){const[a,...m]=l.split("#"),I=m.join("#"),f=pe(J.relative(V.contentDir,a)),{ast:h}=await e.parseMarkdoc({content:S,relativePath:f,isVirtual:P,cacheId:f+(I?"#"+I:"")},i,{sharedDataIds:[`${N}${s}`]});d[`x-parsed-md-${u}`]={result:K(h)}}},name:"openapi-markdoc-inline-parser"});for(const[p,r]of Object.entries(A))await e.createSharedData(p,{...r,baseSlug:e.getRouteByFsPath(r.fsPath)?.baseSlug},r.hash)}}}export{Ce as openAPIDocsPlugin};
