// ci-generator/index.ts — Public API for the CI workflow generator (SPEC-066)
import { buildClaudeCommand, getJobPermissionMode } from './output-formats.js';
import { detectCIStack } from './context-builders.js';
import { buildWorkflow, serializeWorkflow } from './yaml-builder.js';
const DEFAULT_JOBS = ['lint', 'test', 'build'];
const DEFAULT_TRIGGERS = ['push', 'pull_request'];
const DEFAULT_WORKFLOW_NAME = 'CI';
const DEFAULT_SPECS_DIR = 'planu/specs';
/**
 * Build a Claude validate job using plan permission mode (SPEC-257).
 */
function buildClaudeValidateJob(needsJobs) {
    const permMode = getJobPermissionMode('validate');
    const cmd = buildClaudeCommand({
        prompt: 'validate all specs',
        nonInteractive: true,
        permissionMode: permMode,
    });
    const steps = [
        { name: 'Checkout code', uses: 'actions/checkout@v4' },
        {
            name: 'Validate specs with Claude',
            run: cmd,
            env: { ANTHROPIC_API_KEY: '${{ secrets.ANTHROPIC_API_KEY }}' },
        },
    ];
    return {
        name: 'Claude Spec Validation',
        runsOn: 'ubuntu-latest',
        needs: needsJobs,
        steps,
    };
}
/**
 * Generate a GitHub Actions CI workflow for a project.
 * Detects the project stack and generates a valid workflow YAML.
 */
export async function generateCI(input) {
    const { projectPath, jobs = DEFAULT_JOBS, triggers = DEFAULT_TRIGGERS, nodeVersion, pythonVersion, goVersion, rustChannel, javaVersion, workflowName = DEFAULT_WORKFLOW_NAME, includeDriftStep = false, specsDir = DEFAULT_SPECS_DIR, driftSeverityThreshold = 'warning', includeClaudeValidateJob = false, } = input;
    // Detect stack from project files, create a mutable copy
    const detected = await detectCIStack(projectPath);
    const stackContext = { ...detected };
    // Override detected versions with explicit user inputs
    if (nodeVersion !== undefined) {
        stackContext.nodeVersion = nodeVersion;
    }
    if (pythonVersion !== undefined) {
        stackContext.pythonVersion = pythonVersion;
    }
    if (goVersion !== undefined) {
        stackContext.goVersion = goVersion;
    }
    if (rustChannel !== undefined) {
        stackContext.rustChannel = rustChannel;
    }
    if (javaVersion !== undefined) {
        stackContext.javaVersion = javaVersion;
    }
    // Build drift options when requested
    const driftOptions = includeDriftStep
        ? { specsDir, threshold: driftSeverityThreshold }
        : undefined;
    // Build the workflow model
    const workflow = buildWorkflow(stackContext, jobs, triggers, workflowName, driftOptions);
    // Add Claude validate job if requested (SPEC-257)
    if (includeClaudeValidateJob) {
        const existingJobs = Object.keys(workflow.jobs);
        workflow.jobs['claude-validate'] = buildClaudeValidateJob(existingJobs);
    }
    // Serialize to YAML
    const yamlContent = serializeWorkflow(workflow);
    return {
        ecosystem: stackContext.ecosystem,
        workflowPath: workflow.filename,
        yamlContent,
        jobs,
        stackContext,
        driftStepIncluded: includeDriftStep,
    };
}
//# sourceMappingURL=index.js.map