// engine/compliance-test-generator/index.ts — SPEC-390: Orchestrator for compliance test generation
import { readFile } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import { mapCriteriaToControls, getControlsForFrameworks } from './control-mapper.js';
import { formatTest } from './test-formatter.js';
function extractCriteria(specContent) {
    const lines = specContent.split('\n');
    return lines
        .filter((line) => /^\s*-\s*\[[ x]\]/.test(line))
        .map((line) => line.replace(/^\s*-\s*\[[ x]\]\s*/, '').trim())
        .filter((line) => line.length > 0);
}
function buildOutputFiles(specId, frameworks, mappedTests, outputFormat) {
    const files = [];
    for (const framework of frameworks) {
        const testsForFramework = mappedTests.filter((t) => t.control.framework === framework);
        if (testsForFramework.length === 0) {
            continue;
        }
        const ext = outputFormat === 'jest' ? '.test.ts' : outputFormat === 'gherkin' ? '.feature' : '.md';
        const filePath = `compliance/${specId}/${framework}${ext}`;
        const content = testsForFramework.map((t) => t.testCode).join('\n');
        files.push({ path: filePath, content });
    }
    return files;
}
function buildCoverageReport(frameworks, mappedTests) {
    const allControls = getControlsForFrameworks(frameworks);
    const coveredIds = new Set(mappedTests.map((t) => t.control.id));
    return allControls.map((c) => ({ controlId: c.id, covered: coveredIds.has(c.id) }));
}
export async function generateComplianceTests(specPath, specId, frameworks, outputFormat = 'jest') {
    // Read spec content
    let specContent = '';
    if (existsSync(specPath)) {
        specContent = await readFile(specPath, 'utf-8');
    }
    // Extract acceptance criteria lines
    const criteria = extractCriteria(specContent);
    // Map criteria to controls
    const mappedTests = mapCriteriaToControls(criteria, frameworks);
    // Format each mapped test
    for (const test of mappedTests) {
        test.testCode = formatTest(test, outputFormat);
    }
    // Build output files (one per framework)
    const generatedFiles = buildOutputFiles(specId, frameworks, mappedTests, outputFormat);
    // Build coverage report
    const coverageReport = buildCoverageReport(frameworks, mappedTests);
    const coveredCount = coverageReport.filter((r) => r.covered).length;
    const totalCount = coverageReport.length;
    return {
        specId,
        frameworks,
        mappedTests,
        outputFormat,
        generatedFiles,
        coverageReport,
        message: `Generated ${mappedTests.length} compliance tests for ${specId}. ` +
            `Control coverage: ${coveredCount}/${totalCount}. ` +
            `Output format: ${outputFormat}.`,
    };
}
//# sourceMappingURL=index.js.map