import { buildCommandContext, } from '../../../app/command-context.js';
import { readGlobalOptions } from '../../shared/shared.utils.js';
import { Command } from 'commander';
import { evaluateSignals } from '../../../projects/split/signals.js';
const SIGNALS = [
    'independently-shippable',
    'no-shared-design-surface',
    'expect-separate-prs',
    'distinct-subsystems',
];
const DEFAULT_DEPENDENCIES = {
    buildCommandContext,
};
function parseFiredSignals(value) {
    if (value.trim().length === 0) {
        return [];
    }
    const validSignals = new Set(SIGNALS);
    return value.split(',').map((raw) => {
        const signal = raw.trim();
        if (!validSignals.has(signal)) {
            throw new Error(`Invalid signal: ${signal}`);
        }
        return signal;
    });
}
export function createEvaluateSignalsCommand(overrides = {}) {
    const dependencies = {
        ...DEFAULT_DEPENDENCIES,
        ...overrides,
    };
    return new Command('evaluate-signals')
        .description('Evaluate oat-project-split trigger signals')
        .requiredOption('--fired <comma-list>', 'Comma-separated fired signals')
        .action((options, command) => {
        const context = dependencies.buildCommandContext(readGlobalOptions(command));
        try {
            const result = evaluateSignals({
                fired: parseFiredSignals(options.fired),
            });
            if (context.json) {
                context.logger.json(result);
            }
            else {
                const status = result.triggered ? 'triggered' : 'below threshold';
                context.logger.info(`Signal evaluation: ${status} (confidence: ${result.confidence})`);
                context.logger.info(`Fired signals: ${result.fired.length > 0 ? result.fired.join(', ') : 'none'}`);
            }
            process.exitCode = 0;
        }
        catch (error) {
            const message = error instanceof Error ? error.message : String(error);
            context.logger.error(message);
            process.exitCode = 1;
        }
    });
}
