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