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