// engine/mutation-config-generator.ts — SPEC-323
// Detects project stack and generates the appropriate mutation testing config file.
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { getMutationTool, generateMutationConfig as buildMutationConfig, } from './advanced-testing/mutation-advisor.js';
// ---------------------------------------------------------------------------
// Stack detection
// ---------------------------------------------------------------------------
async function detectStack(projectPath) {
    const pkgPath = join(projectPath, 'package.json');
    try {
        const raw = await readFile(pkgPath, 'utf8');
        const pkg = JSON.parse(raw);
        const deps = {
            ...(pkg.dependencies ?? {}),
            ...(pkg.devDependencies ?? {}),
        };
        const language = 'typescript' in deps ? 'typescript' : 'javascript';
        let testFramework = 'unknown';
        if ('vitest' in deps) {
            testFramework = 'vitest';
        }
        else if ('jest' in deps) {
            testFramework = 'jest';
        }
        else if ('mocha' in deps) {
            testFramework = 'mocha';
        }
        return { language, testFramework };
    }
    catch {
        const hasPython = (await readFile(join(projectPath, 'setup.py'), 'utf8').catch(() => null)) !== null ||
            (await readFile(join(projectPath, 'pyproject.toml'), 'utf8').catch(() => null)) !== null;
        if (hasPython) {
            return { language: 'python', testFramework: 'pytest' };
        }
        const hasMaven = (await readFile(join(projectPath, 'pom.xml'), 'utf8').catch(() => null)) !== null;
        if (hasMaven) {
            return { language: 'java', testFramework: 'junit' };
        }
        return { language: 'typescript', testFramework: 'unknown' };
    }
}
// ---------------------------------------------------------------------------
// Config file name resolution
// ---------------------------------------------------------------------------
function resolveConfigFileName(tool) {
    switch (tool) {
        case 'stryker':
            return 'stryker.config.mjs';
        case 'mutmut':
        case 'cosmic-ray':
            return 'setup.cfg';
        case 'pitest':
            return 'pom.xml (add mutation plugin section)';
        case 'cargo-mutants':
            return '.cargo-mutants.toml';
        case 'gremlins':
            return '.gremlins.yaml';
        case 'stryker4s':
            return 'stryker4s.conf';
        case 'stryker-dotnet':
            return 'stryker-config.json';
        default:
            return 'stryker.config.mjs';
    }
}
// ---------------------------------------------------------------------------
// CI step generation
// ---------------------------------------------------------------------------
function buildCiStep(tool) {
    switch (tool) {
        case 'stryker':
            return 'npx stryker run';
        case 'mutmut':
            return 'python -m mutmut run && python -m mutmut results';
        case 'pitest':
            return 'mvn test-compile org.pitest:pitest-maven:mutationCoverage';
        case 'cargo-mutants':
            return 'cargo mutants';
        case 'gremlins':
            return 'gremlins unleash ./...';
        case 'stryker4s':
            return 'sbt stryker';
        case 'stryker-dotnet':
            return 'dotnet stryker';
        default:
            return 'npx stryker run';
    }
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Detect project stack and generate the appropriate mutation testing config.
 */
export async function generateMutationConfig(projectPath) {
    const stack = await detectStack(projectPath);
    const tool = getMutationTool(stack.language);
    const mutationConfig = buildMutationConfig(tool, stack.language);
    const configFileName = resolveConfigFileName(tool);
    const ciStep = buildCiStep(tool);
    return {
        configFileName,
        configContent: mutationConfig.configFile,
        ciStep,
        framework: stack.testFramework,
        tool,
    };
}
//# sourceMappingURL=mutation-config-generator.js.map