import E from"@redocly/portal-plugin-mock-server";import B from"path";import{REDOCLY_ROUTE_RBAC as x}from"@redocly/config";import{combineUrls as w,withPathPrefix as A}from"@redocly/theme/core/utils";import{VERSION_SEPARATOR as O}from"../constants/common.js";import{reporter as S}from"../utils/reporter/reporter.js";import{customPagesPlugin as N}from"../../server/plugins/pages/index.js";import{openAPIDocsPlugin as k}from"../../server/plugins/openapi-docs/index.js";import{asyncAPIDocsPlugin as L}from"../../server/plugins/asyncapi-docs/index.js";import{sidebarsPlugin as V}from"../../server/plugins/sidebars/index.js";import{configParserPlugin as I}from"./config-parser/index.js";import{markdownPlugin as _}from"./markdown/index.js";import{generateBrowserPluginsModule as j,generateClientRoutes as M,generateTemplatesModule as $}from"../esbuild/generate.js";import{graphqlDocsPlugin as z}from"./graphql-docs/index.js";import{searchPlugin as U}from"./search/index.js";import{defaultThemePlugin as q}from"./default-theme/index.js";import{apiKeyMgmtPlugin as G}from"./dev-onboarding/index.js";import{apiFunctionsPlugin as K}from"./api-functions/index.js";import{catalogClassicPlugin as H}from"./catalog-classic/index.js";import{scorecardPlugin as W}from"./scorecard/index.js";import{resolvePlugins as Y}from"../config/external-plugins.js";import{l10nPlugin as J}from"./l10n/index.js";import{analyticsPlugins as Q}from"./analytics/index.js";import{sitemapPlugin as X}from"./sitemap/index.js";import{entitlementsPlugin as Z}from"./entitlements/index.js";import{getBilledPagesCount as ee,logger as u,shaDirPathShort as te,slug as oe}from"../utils/index.js";import{telemetry as ne}from"../../cli/telemetry/index.js";import{EntitlementsProvider as re}from"../entitlements/entitlements-provider.js";import{ssoPlugin as ie}from"./sso/index.js";import{Cache as se}from"../fs/cache.js";import{ContentFs as ae}from"../fs/content-fs.js";import{normalizeRouteSlug as F,removeTrailingSlash as T,slash as le}from"../../shared/utils.js";import{isTruthy as ce}from"../../shared/jsUtils.js";import{parsePathVersions as ue}from"./config-parser/loaders/versions-config-loader.js";import{findProductBySlug as fe}from"../utils/product.js";import{isDevelopMode as me}from"../utils/is-develop-mode.js";import{parseBaseName as ge}from"./utils.js";const de=[I,ie,q,J,_,k,L,W,K,N,z,G,H,V,X,Z,U,E,...Q];async function tt(e,t=de){const n=[],y=re.instance(),f={};for(const C of t){const o=await C(e);for(const[i,m]of Object.entries(o.loaders||{}))f[i]&&S.panicOnBuild(`Duplicate loader with name ${i}`),f[i]=m;(!o.requiredEntitlements||o.requiredEntitlements?.every(i=>y.canAccessFeature(i)))&&n.push(o)}const s=new ae(e.contentDir),R=new se(s);return R.setLoaders(f),u.verbose("All plugins instantiated"),{pluginInstances:n,lifecycleContext:he(s,R)}}function D(e){u.verbose("Generating templates"),$(e),j(e),M(e)}function pe(e){const t=e.getAllRoutes();ee(t)||S.panicOnBuild("No routes created by plugins. Please check your project configuration.")}async function ot(e,t,n,y={}){try{t.startPluginsRun();for(const o of e){const i=u.startTiming();await o.processContent?.(t,n),u.verboseTime(i,`processContent for ${o.id}`)}const s=(await Y(t.contentDir,t.config.plugins)).map(o=>o.lifecyclePlugin).filter(ce);n.cache.setLoaders(Object.fromEntries(s.flatMap(o=>Object.entries(o.loaders||{}))));for(const o of s){const i=u.startTiming();await o.processContent?.(t,n),u.verboseTime(i,`processContent for ${o.id}`)}const R=await Pe(t,n),C=[...e,...s];D(t),me()?await t.userCodeReady:t.buildRevision++;for(const o of C){const i=u.startTiming();await o.afterRoutesCreated?.(t,R),u.verboseTime(i,`afterRoutesCreated for ${o.id}`)}pe(t),D(t)}catch(f){const s="Unhandled error in plugin. "+f.message+`
`+f.stack;ne.send("cli_error_caught",{message:s}),y.failFast?u.panic(s):S.panicOnBuild(s)}finally{t.finishPluginsRun()}}async function Pe(e,t){const{cache:n,fs:y}=t,f=await t.getConfig(),s=Object.values(f.products||{}),R=(await n.load("versions-config","versions-config")).data,C=(await n.load("content-slugs","content-slugs")).data;for(const{duplicateInAllLocales:m,slugSuffix:d,sharedData:p=[],redirectFrom:P=[],...l}of e.newRoutes){const a=F(l.slug?l.slug:o(l.fsPath)),h=T(d?w(a,d):a),r={...l,[x]:{slug:h,fsPath:l.fsPath},slug:h,baseSlug:a,versions:i(l.fsPath,d),product:fe(s,h,o)};e.routesByFsPath.set(r.fsPath,r.slug),e.routesBySlug.set(r.slug,r);for(const{key:g,id:c}of p)e.addRouteSharedData(r.slug,g,c);for(const g of P)e.addRedirect(g.from,{type:g.type,to:r.slug});if(m)for(const g of y.localeFolders){const c={...r,slug:"/"+g.toLowerCase()+r.slug};e.routesByFsPath.set(c.fsPath,c.slug),e.routesBySlug.set(c.slug,c);for(const{key:v,id:b}of p)e.addRouteSharedData(c.slug,v,b);for(const v of P){const b=w(y.localizationFolder,g,v.from);e.addRedirect(b,{type:v.type,to:c.slug})}}}return e.newRoutes=[],{...t,slugify:o};function o(m){const d=le(m).replace(new RegExp("^(@i18n|@l10n)\\/"),""),p=C.fileSlugs.get(d)||C.dirSlugs.get(T(d));if(p)return p;const{baseName:P,isIndexFile:l}=ge(m);let a=B.dirname(m.replace(new RegExp("^(@i18n|@l10n)\\/"),""));return a=a==="."?"/":a,w("/",oe(l?a:w(a,P)),"/")}function i(m,d=""){const p=ue(m);if(!p?.versionName)return;const{versionFolderPath:P,filePathInVersion:l,versionName:a}=p,h=R.get(P);if(h)return h.versions.map(r=>{const g=h.defaultVersion===r.version,c=w(P,O+r.version,l),v=o(c);return{version:r.version,label:r.name||r.version,link:F(w(v,d)),default:g,active:a===r.version,folderId:te(`${P}`)}})}}function he(e,t){return{fs:e,cache:t,getConfig:async(n=".")=>(await t.load(n,"nearest-redocly-config",e.localeFolders)).data,isPathIgnored:async n=>(await t.load(n,"is-ignored")).data,withPathPrefix:A,logger:u}}export{de as INTERNAL_PLUGINS,he as createLifecycleContext,tt as initPlugins,ot as runPlugins,D as writeClientEntries};
