import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {C} from './design-lib.mjs';
import {darkPalette} from './render.mjs';
const root=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..');
const lum=hex=>{const c=[1,3,5].map(i=>parseInt(hex.slice(i,i+2),16)/255).map(v=>v<=.04045?v/12.92:((v+.055)/1.055)**2.4);return c[0]*.2126+c[1]*.7152+c[2]*.0722;};
const pairs=[['ink','paper'],['muted','soft'],['subtle','canvas'],['green','greenSoft'],['amber','amberSoft'],['red','redSoft'],['blue','blueSoft'],['purple','purpleSoft'],['onPrimary','green'],['onDanger','red'],['navMuted','nav'],['controlBorder','paper',3]];
const checks=[];for(const [theme,palette]of Object.entries({light:C,dark:darkPalette}))for(const [fg,bg,required=4.5]of pairs){const l=[lum(palette[fg]),lum(palette[bg])].sort((a,b)=>b-a),ratio=(l[0]+.05)/(l[1]+.05);checks.push({theme,foreground:fg,background:bg,ratio:Number(ratio.toFixed(2)),required,passed:ratio>=required});}
const failures=checks.filter(x=>!x.passed);const report={scope:'Selected semantic color pair checks; not a complete application accessibility audit',checks,failures};fs.writeFileSync(path.join(root,'qa/contrast-report.json'),JSON.stringify(report,null,2));
const build=JSON.parse(fs.readFileSync(path.join(root,'qa/build-report.json'),'utf8'));if(build.missingTargets.length||build.screenOverflow.length)throw new Error('Build layout or link failures');if(failures.length)throw new Error('Semantic contrast failures: '+JSON.stringify(failures));console.log(JSON.stringify({contrastPairs:checks.length,failures:0,frames:build.frames,routeCoverage:build.coveredRoutes}));
