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