import a from"path";import{productConfigOverrideSchema as R,REDOCLY_TEAMS_RBAC as w,REDOCLY_ROUTE_RBAC as O}from"@redocly/config";import{CONFIG_FILE_NAME as b}from"../../../constants/common.js";import{findDeepFirst as A}from"../../../utils/tree/find-deep-first.js";import{removeTrailingSlash as B}from"../../../utils/url/remove-trailing-slash.js";import{addTrailingSlash as D}from"../../../utils/url/add-trailing-slash.js";import{reporter as k}from"../../tools/notifiers/reporter.js";import{logger as F}from"../../tools/notifiers/logger.js";import{FileNotFoundError as E,resolveAssetPath as I}from"../../utils/index.js";import{safeParsePartial as _}from"../config-parser/safe-parse.js";import{resolveLinksFromConfig as v,resolveItems as S}from"../nav-utils.js";import{resolveLogoConfig as j}from"./resolve-logo.js";import{pickUiAccessibleConfig as z}from"./index.js";import{getExcludedFromLinkCheckerPatterns as N}from"../sidebars/utils.js";const re=async(o,e,r)=>{if(!o)return;const s={};for(const t of Object.keys(o)){let d;const m=o[t].icon;if(m)try{d=await I(m,r.fs,{fromFileRelativePath:b,contentDir:e.contentDir,outdir:e.outdir})}catch(L){L instanceof E?await k.panicOnBuildContentError(`Could not resolve icon for product "${t}": file ${m} does not exist`):await k.panicOnBuild(`Could not resolve icon for product "${t}": ${L.message}`)}const{name:C,folder:n}=o[t],l=a.posix.join(n,"redocly.yaml"),f=r.fs.exists(l)?await r.cache.load(l,"product-config"):void 0,c=f?.data&&_(R,f.data),p=await T(n,r,e);if(!p){await k.panicOnBuild(`Could not resolve first link for product "${t}"`);continue}const u=e.getRouteBySlug(p),g=a.join(e.contentDir,n),i=c&&z(c),P=Object.keys(i?.logo||{}).length>0?await j(i?.logo,l,e.outdir,r.fs):void 0,h=N(e.getConfig()),y={navFile:l,excludedFromLinkCheckerPatterns:h};s[t]={name:C,folder:n,icon:d,slug:r.slugify(a.normalize(n)),link:p,configOverride:i&&{logo:Object.keys(P||{}).length>0?P:void 0,navbar:await v(i.navbar,g,e,r,y),footer:await v(i.footer,g,e,r,y),feedback:i.feedback,sidebar:i.sidebar,search:i.search&&{...i.search,suggestedPages:await v(i.search?.suggestedPages,g,e,r,{navFile:b,excludedFromLinkCheckerPatterns:h})},codeSnippet:i.codeSnippet,breadcrumbs:i.breadcrumbs&&{...i.breadcrumbs,prefixItems:await v(i.breadcrumbs?.prefixItems||[],g,e,r,{navFile:b,excludedFromLinkCheckerPatterns:h})},palette:i.palette,analytics:i.analytics,mockServer:i.mockServer},[w]:u?.[w],[O]:u?.[O]},F.verbose(`Resolved config for product "${t}"`)}return s};async function T(o,e,r){const s=D(a.posix.normalize(o)),t=B(e.slugify(a.normalize(o))),d=r.getRouteBySlug(t);if(d&&d.fsPath.startsWith(s))return d.slug;const m=e.fs.scan(/sidebars\.yaml$/).filter(c=>c.relativePath.startsWith(s)),n=W(m)[0]?.relativePath;if(n){const c=await e.cache.load(n,"yaml"),p=await S(c.data,a.dirname(n),r,e,{navFile:n}),u=$(p);if(u)return F.verbose(`Found product first link "${u}" in sidebar "${n}"`),u}const l=await S([{directory:`${s}`}],a.dirname("sidebars.yaml"),r,e,{navFile:"sidebars.yaml"}),f=$(l);return f&&F.verbose(`Found product first link "${f}" in product folder "${s}"`),f}function $(o){return(o?A(o,r=>!!r.link):void 0)?.link}function W(o){return o.sort((e,r)=>{const s=e.relativePath.split("/").length,t=r.relativePath.split("/").length;return s!==t?s-t:e.relativePath.localeCompare(r.relativePath,void 0,{numeric:!0})})}export{T as getProductFirstLink,re as resolveProductsConfig};
