import S from"path";import{REDOCLY_ROUTE_RBAC as g,REDOCLY_TEAMS_RBAC as b}from"@redocly/config";import{deepMerge as v}from"../../../utils/object/deep-merge.js";import{buildWildcardRedirectsTree as P}from"../../utils/redirects/build-wildcard-redirects-tree.js";import{shaHexShort as D}from"../../utils/crypto/sha-hex-short.js";import{formatCustomScripts as u,formatCustomLinks as T}from"./format-custom-tags.js";import{applyL10nToRbacConfig as w,resolveDirectoryHashes as L}from"../../utils/rbac.js";import{copySeoAssets as O}from"./copy-seo-assets.js";import{normalizeRedirectSources as k}from"./normalize-redirect-sources.js";import{applyL10nToRedirects as E}from"./apply-l10n-to-redirects.js";import{DEFAULT_LOADERS as G}from"./loaders/index.js";import{resolveSearchFacets as _}from"./resolve-search-facets.js";import{telemetryTraceStep as B}from"../../../cli/telemetry/helpers/trace-step.js";import{transformMdAst as j}from"../markdown/runtime-transform.js";async function V(){return{id:"Config Parser",loaders:G,async processContent(e,{getConfig:i,fs:t}){await B("build.plugin.config_parser",async o=>{const{contentDir:s,setGlobalConfig:a,setGlobalData:l}=e,r=await i(),n=r.access||{},d=w(t,n.rbac||{}),f={outdir:e.outdir,contentDir:s},y=await u(r.scripts?.head,f),h=await T(r.links,f),R=await u(r.scripts?.body,f),p=v(r,{headScriptTags:y,linkTags:h,postBodyScriptTags:R});o?.setAttribute("config",JSON.stringify(p));const m=r.redirects?k(r.redirects):{},C=Object.keys(m),A=p.banner?.map(c=>typeof c=="object"&&c!==null&&c.rbac?{...c,[b]:c.rbac}:c);a({...p,banner:A,access:{...n,requiresLogin:!!n.requiresLogin,rbac:d},directoryPaths:await L(t,n.rbac),seo:r.seo&&await O(r.seo,f.contentDir,f.outdir),redirects:E(m,r.l10n),wildcardRedirectsTree:P(m),originalRedirectSources:C}),_(r.search?.filters?.facets||[],e.getSearchFacets,e.setSearchFacets),l({removeAttribution:!!r.removeAttribution})})},async afterRoutesCreated(e,i){x(e);const t=e.getGlobalConfig("banner");if(Array.isArray(t)){const o=await Promise.all(t.map(async(s,a)=>{const{content:l,...r}=s,{ast:n}=await e.parseMarkdoc({input:{content:l,relativePath:"redocly.yaml"},context:i,resource:`redocly.yaml#banner${a}`}),{renderableAst:d}=await j(n,{variables:{},partials:{}},e.serverOutDir);return{...r,ast:d,hash:D(l)}}));e.setGlobalConfig({banner:o})}}}}function x(e){const i=e.getGlobalConfig("access")?.rbac,t=e.getGlobalConfig("directoryPaths");if(!(!i||Object.keys(i).length===0))for(const o of e.getAllRoutes()){if(o.versions&&t)for(const s of o.versions){const a=t[s.folderId],r=a==="."||a==="/"?"":a;s[g]={slug:s.link,fsPath:S.posix.join(r,"@"+s.version,"index.md")}}o[g]||o[b]||(o[g]={slug:o.slug,fsPath:o.fsPath})}}export{V as configParserPlugin};
