import w from"node:path";import{regexFromString as h}from"@redocly/openapi-core";import{reporter as x}from"../../../tools/notifiers/reporter.js";import{lintDefinition as C}from"../lint.js";import{ExternalResolver as N}from"../../../fs/utils/external-ref-resolver.js";const I="Non-conformant";async function F(t,e){const{data:{levelsConfig:n,configs:i,targets:r}}=await e.cache.load("","scorecard-config"),o={};let s=I,d=!1;const m=await e.fs.read(t),{data:f}=await e.cache.load(t,"yaml"),g=new N(e.fs);let l={};if(r.length){const a=await e.getConfig(w.posix.dirname(t)),c=f?.info?.["x-metadata"],p={title:f.info.title,version:f.info.version,...c,...a.metadata};l=(await M(r,p))?.configs||{}}for(const a of n){const c=await C({parsed:f,content:m,relativePath:t},l[a.name]||i[a.name],e,g);o[a.name]=c,c.errors===0&&!d?s=a.name:d=!0}let u=n.findIndex(a=>a.name===s);return{levels:o,scorecardLevel:s,scorecardLevelIdx:u+1}}async function M(t,e){if(t)for(const n of t){const i=Object.entries(n.where?.metadata||{});let r=!0;for(const[o,s]of i)if(!await O(o,s,e)){r=!1;break}if(r)return n}}async function O(t,e,n){if(e.match(/^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}Z)?\/(\d{4}-\d{2}-\d{2})?$/)){if(!n[t])return!1;const[i,r]=e.split("/"),o=new Date(n[t]);return isNaN(o.getTime())?!1:!(o<new Date(i)||r&&o>new Date(r))}else if(e.match(/^\/.*\//)){if(!n[t])return!1;try{return h(e).test(n[t])}catch{return await x.panicOnContentError(`Invalid regex in scorecard target "${t}": ${e}`),!1}}return n[t]===e}export{M as getTarget,F as scorecardLoader};
