import w from"node:path";import R from"picomatch";import{removeLeadingSlash as S}from"@redocly/theme/core/utils";import{parsePathVersions as O}from"../../../utils/path/parse-path-versions.js";import{logger as l}from"../../tools/notifiers/logger.js";import{ScorecardStatus as d}from"./types.js";import{getTarget as M}from"./loaders/scorecard.js";const P=15;async function y({actions:t,context:i,scorecardConfig:e,reuniteConfig:o}){l.info("Computing scorecard...");const a=e.levels||[],n=a[0],u=await t.loadOpenApiDefinitions(i),s=e.ignore||[],h=R(s),f=new Set,g=l.startTiming(),c={},p={};for(const r of u){if(!D(r,h,f))continue;const v=r.customOutputRelativeFile||r.realRelativePath;f.add(v);const m=await N(r,i,e,a,n);c[r.realRelativePath]=m,p[m.relativePath]=m}return l.infoTime(g,"Scorecard processed"),T(c,o,e),p}function D(t,i,e){if(t.isVirtual||!t.definition)return!1;const o=t.customOutputRelativeFile||t.realRelativePath,a=S(t.realRelativePath);return i(a)?(l.info(`Skipping scorecard calculation for ignored file: ${t.realRelativePath}`),!1):!e.has(o)}function L(t,i,e){return t-1<i?d.BelowMinimum:i<e-1?d.Minimum:d.Highest}async function N(t,i,e,o,a){let n=a.name;if(e.targets?.length){const r=await i.getConfig(w.posix.dirname(t.realRelativePath)),v=t.definition.info?.["x-metadata"]||{},m={title:t.definition.info?.title,version:t.definition.info?.version,...v,...r.metadata};n=(await M(e.targets,m))?.minimumLevel||a.name}const{data:{levels:u,scorecardLevelIdx:s,scorecardLevel:h}}=await i.cache.load(t.realRelativePath,"scorecard"),f=o.findIndex(r=>r.name===n),g=L(s,f,o.length),c=t.customOutputRelativeFile||t.realRelativePath,p=O(c)?.versionName;return{levels:u,status:g,targetLevel:n,scorecardLevel:h,scorecardLevelIdx:s,relativePath:c,title:t.definition.info?.title,version:p}}function T(t,i,e){const o=Object.values(t).filter(s=>s.status===d.BelowMinimum);if(o.length===0)return;const a=o.map(s=>s.relativePath);let n=`Detected OpenAPI definitions with score below minimum level:
  - ${a.slice(0,P).map(()=>"%rp").join(`
  - `)}`;a.length>P&&(n+=`
  ... and ${a.length-P} more`),i?.ignoreLint??e.ignoreNonCompliant?l.warn(n,...a):l.error(n,...a)}export{y as computeScorecard};
