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