import{writeFileSync as E}from"node:fs";import R from"path";import{SCORECARD_OUTPUT_FILE_NAME as L}from"../../constants/common.js";import{mapObject as h}from"../../../utils/object/map-object.js";import{readEnvVariable as P}from"../../utils/envs/read-env-variable.js";import{removeLeadingSlash as T}from"../../../utils/url/remove-leading-slash.js";import{combineUrls as y}from"@redocly/theme/core/utils";import{logger as _}from"../../tools/notifiers/logger.js";import{ensureDir as b}from"../../utils/index.js";import{getTemplatePath as A}from"./get-template-path.js";import{getAllRuleNames as F}from"./lint.js";import{getScorecardConfig as O}from"./get-scorecard-config.js";import{CUSTOM_FIELDS_SERVER_PROPS_GETTER_ID as j}from"../catalog-classic/index.js";import{getScorecardSlug as w}from"./shared-utils.js";import{scorecardConfigLoader as N}from"./loaders/scorecard-config.js";import{scorecardLoader as U}from"./loaders/scorecard.js";import{computeScorecard as q}from"./compute-scorecard.js";import{telemetryTraceStep as M}from"../../../cli/telemetry/helpers/trace-step.js";const k="scorecard";async function ae({id:D,requiredEntitlements:I,loadersPrefix:p=""}){let d=null,S=null,v=!1;return{id:D??"scorecard",requiredEntitlements:I??["scorecard"],loaders:{[`${p}scorecard-config`]:N,[`${p}scorecard`]:U},async processContent(t,i){await M("build.plugin.scorecard",async u=>{const g=t.createTemplate(k,A("./template/index.js")),l=await i.getConfig(),a=O(l);u?.setAttribute("config",JSON.stringify(a||{}));const m=l?.reunite??{},s=l.catalogClassic??{},c=a.levels||[];if(c.length<1)throw Error("Invalid configuration of levels config");d===null&&(d=await q({actions:t,context:i,scorecardConfig:a,reuniteConfig:m}));const{data:{configs:e}}=await i.cache.load("",`${p}scorecard-config`),{perLevel:n,all:C}=F(e);S={levelNames:c.map(o=>o.name),rules:C,rulesPerLevel:n};for(const[o,r]of Object.entries(s)){const f=y(r.slug,"scorecard/");t.addRoute({duplicateInAllLocales:!0,slug:f,fsPath:y(r.slug,"scorecard/"),templateId:g,hasClientRoutes:!0,excludeFromSidebar:!0,serverPropsGetterIds:[j],sharedData:[{id:"scorecard",key:"scorecard"},{key:"catalog",id:"catalog-"+o}],getNavText:()=>Promise.resolve(r.title||"API design scorecard"),getStaticData:async()=>({props:{catalogId:o,catalogConfig:{...r,items:void 0},scorecardConfig:a,scorecardInfo:S}})})}})},afterRoutesCreated:async(t,{getConfig:i})=>{const u=await i(),g=u.catalogClassic??{},l=O(u);if(Object.keys(g).length){const s={};for(const[c,e]of Object.entries(d||{})){const n=t.getRouteByFsPath(c),C=y(Object.values(g)[0].slug,"scorecard/"),o=t.getRouteByFsPath(c)?.slug;if(o&&(n&&!l.ignoreMetadata&&(n.metadata={...n.metadata,scorecardStatus:e.status,scorecardLevel:e.scorecardLevel,scorecardLevelIdx:e.scorecardLevelIdx,scorecardLevels:h(e.levels,r=>({uniqueErrors:r.uniqueErrors,uniqueWarnings:r.uniqueWarnings})),scoreCardSlug:C+"apis/"+encodeURIComponent(T(o))}),s[o]={...e,levels:h(e.levels,r=>({...r,problems:r.problems.map(f=>({ruleId:f.ruleId,severity:f.severity}))}))},!v)){const r=R.resolve(t.outdir,"_scorecard",w(o));E(b(r),JSON.stringify(e))}}await t.createSharedData("scorecard",s)}const a={};for(const[s,c]of Object.entries(d||{})){const e=t.getRouteByFsPath(s)?.slug;e&&(a[e]=c)}const m=P("REDOCLY_METADATA_OUTPUT_FOLDER");!v&&m&&(_.info("Writing scorecard data..."),E(R.join(m,L),JSON.stringify({scorecardData:a,...S}))),v=!0}}}export{ae as scorecardPlugin};
