import { Args, Command, Flags } from '@oclif/core';
import chalk from 'chalk';
import { rejectOrigin } from '../../origin/domain/actions.js';
import { discoverOrigins } from '../../origin/domain/origin-file.js';
export default class OriginReject extends Command {
    static args = {
        pathspec: Args.string({
            description: 'Optional file or directory. Defaults to recursive cwd scan.',
            required: false,
        }),
    };
    static description = 'Reject the codegen proposal: delete `.origin` and keep the current file as-is.';
    static examples = [
        '<%= config.bin %> origin reject',
        '<%= config.bin %> origin reject backend/foo.ts',
        '<%= config.bin %> origin reject backend/iam/',
        '<%= config.bin %> origin reject --json',
    ];
    static flags = {
        json: Flags.boolean({
            default: false,
            description: 'Emit machine-readable JSON instead of human text.',
        }),
    };
    async run() {
        const { args, flags } = await this.parse(OriginReject);
        const origins = discoverOrigins(args.pathspec);
        if (origins.length === 0 && args.pathspec) {
            const results = [rejectOrigin(args.pathspec)];
            this.emit(results, flags.json);
            return;
        }
        const results = origins.map((entry) => rejectOrigin(entry.current));
        this.emit(results, flags.json);
    }
    emit(results, asJson) {
        if (asJson) {
            this.log(JSON.stringify({ count: results.length, results }, null, 2));
            return;
        }
        if (results.length === 0) {
            this.log('No origin files to reject.');
            return;
        }
        for (const r of results) {
            if (r.status === 'applied') {
                this.log(`${chalk.yellow('[REJECTED]')} ${r.path}`);
            }
            else {
                this.log(`${chalk.dim('[NOOP]')} ${r.path} — ${r.reason ?? 'no change'}`);
            }
        }
    }
}
