// engine/figma/visual-qa.ts — Visual QA comparison engine (SPEC-429)
const FIGMA_API_BASE = 'https://api.figma.com/v1';
// ---------------------------------------------------------------------------
// exportFigmaFrameImage — export a Figma frame as a PNG CDN URL
// ---------------------------------------------------------------------------
export async function exportFigmaFrameImage(fileKey, nodeId, accessToken) {
    const url = `${FIGMA_API_BASE}/images/${fileKey}?ids=${encodeURIComponent(nodeId)}&format=png&scale=2`;
    const response = await fetch(url, {
        headers: { 'X-Figma-Token': accessToken },
    });
    if (!response.ok) {
        throw new Error(`Failed to export Figma frame: ${response.status}`);
    }
    const data = (await response.json());
    const imageUrl = data.images[nodeId];
    if (imageUrl === null || imageUrl === undefined) {
        throw new Error('Figma could not export frame: ' + nodeId);
    }
    return imageUrl;
}
// ---------------------------------------------------------------------------
// createVisualQAResult — build a VisualQAResult from diffs
// ---------------------------------------------------------------------------
export function createVisualQAResult(specId, nodeId, figmaImageUrl, screenshotUrl, diffs) {
    const criticalCount = diffs.filter((d) => d.severity === 'critical').length;
    const warningCount = diffs.filter((d) => d.severity === 'warning').length;
    const infoCount = diffs.filter((d) => d.severity === 'info').length;
    return {
        specId,
        nodeId,
        figmaImageUrl,
        screenshotUrl,
        comparedAt: new Date().toISOString(),
        diffs,
        criticalCount,
        warningCount,
        infoCount,
        passed: criticalCount === 0,
    };
}
// ---------------------------------------------------------------------------
// buildPlaceholderDiff — placeholder for pixel-level image comparison
// ---------------------------------------------------------------------------
export function buildPlaceholderDiff(_screenshotUrl, _figmaImageUrl) {
    // TODO: Integrate with canvas/Playwright for pixel-level comparison
    return [];
}
// ---------------------------------------------------------------------------
// formatVisualQAReport — render results as a markdown table
// ---------------------------------------------------------------------------
export function formatVisualQAReport(results) {
    if (results.length === 0) {
        return 'No visual QA results yet. Run validate_figma_implementation to compare designs with implementation.';
    }
    const header = '| Spec | Status | Critical | Warnings | Compared At |';
    const separator = '|------|--------|---------|---------|------------|';
    const rows = results.map((r) => {
        const status = r.passed ? '✅ Pass' : '❌ Fail';
        const date = r.comparedAt.slice(0, 10);
        return `| ${r.specId} | ${status} | ${r.criticalCount} | ${r.warningCount} | ${date} |`;
    });
    return [header, separator, ...rows].join('\n');
}
// ---------------------------------------------------------------------------
// buildSummary — aggregate summary across all results
// ---------------------------------------------------------------------------
export function buildSummary(results, totalSpecs) {
    return {
        totalSpecs,
        specsValidated: results.length,
        specsWithCritical: results.filter((r) => r.criticalCount > 0).length,
        specsNeverValidated: totalSpecs - results.length,
        validatedAt: new Date().toISOString(),
    };
}
//# sourceMappingURL=visual-qa.js.map