import h from"path";import{lintConfig as C,loadConfig as P,createConfigTypes as T}from"@redocly/openapi-core";import{deepMerge as b}from"../../../../../utils/object/deep-merge.js";import{readEnvVariable as R}from"../../../../utils/envs/read-env-variable.js";import{logger as c}from"../../../../tools/notifiers/logger.js";import{safeParsePartial as A}from"../../safe-parse.js";import{formatConfigProblem as x}from"../../format-error.js";import{ExternalResolver as E}from"../../../../fs/utils/external-ref-resolver.js";import{resolveMutuallyExclusiveProps as $}from"../../resolve-mutual-exclusion.js";async function W(o,r,t,a){const u=(await r.getFileInfo(o))?.realRelativePath||o;async function y(){const i=new E(r),l=h.join(r.cwd,u),n=await P({configPath:l,externalRefResolver:i}),f=await a(n.resolvedConfig);if(f===void 0)return n.resolvedConfig;const g=[...n.document?.source?await C({config:n,externalConfigTypes:T(f,n)}):[],...n.document?.source?$(n.resolvedConfig,n.document?.source):[]];if(g.length>0)for(const w of g)t(new Error(x(w,r.cwd)));return n.resolvedConfig}const s=await r.exists(o)?await y():{},v=j(s),d=await a(s);"residency"in s&&s.residency&&typeof s.residency=="string"&&(/^https?:\/\/.*/.test(s.residency)||t(new Error(`Invalid residency URL: "${s.residency}". Residency must start with "http://" or "https://".`)));let p=d?A(d,s):s;const{env:m}=p;if(m){const i=R("REDOCLY_ENV")||"development",l=m[i]||{};p=b(p,l)}const e=M(p,v);if(e.imports&&e.imports.length>0){c.warn("The 'imports' property is deprecated. Please use 'plugins' property instead.");const i=new Set([...e.plugins||[],...e.imports.map(l=>h.posix.join(l,"plugin.js"))]);e.plugins=Array.from(i),delete e.imports}if(e.catalog&&(c.warn("The 'catalog' property is deprecated. Please use 'catalogClassic' property instead."),e.catalogClassic={...e.catalog},delete e.catalog),e.search?.ai){c.warn("The 'search.ai' property is deprecated. Please use 'aiAssistant' property instead.");const i={...e.search?.ai,...e.aiAssistant,suggestions:e.aiAssistant?.suggestions?.length?e.aiAssistant.suggestions:e.search?.ai.suggestions||[]};e.aiAssistant=i,delete e.search.ai}return e}function j(o){if(!o.theme)return[];c.warn("The 'theme' property in redocly.yaml is deprecated. Please move all of the properties from 'theme' to the root of the config.");const r=[];for(const t of Object.keys(o.theme))o[t]==null?r.push(t):c.warn(`Detected both '${t}' and 'theme.${t}' properties in redocly.yaml. The 'theme.${t}' property will be ignored and needs to be removed or merged into the '${t}'.`);return r}function M(o,r){if(!o.theme||r.length===0)return o;const t={...o};for(const a of r)t[a]=o.theme[a];return delete t.theme,t}export{W as readAndValidateConfig};
