import { Args, Command, Flags } from '@oclif/core';
import chalk from 'chalk';
import { ignoreOrigin } from '../../origin/domain/actions.js';
import { discoverOrigins } from '../../origin/domain/origin-file.js';
export default class OriginIgnore extends Command {
    static args = {
        pathspec: Args.string({
            description: 'Optional file or directory. Defaults to recursive cwd scan.',
            required: false,
        }),
    };
    static description = 'Reject the proposal AND mark the current file as ignored on future regens.';
    static examples = [
        '<%= config.bin %> origin ignore',
        '<%= config.bin %> origin ignore backend/foo.ts',
        '<%= config.bin %> origin ignore backend/iam/',
        '<%= config.bin %> origin ignore --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(OriginIgnore);
        const origins = discoverOrigins(args.pathspec);
        if (origins.length === 0 && args.pathspec) {
            const results = [ignoreOrigin(args.pathspec)];
            this.emit(results, flags.json);
            return;
        }
        const results = origins.map((entry) => ignoreOrigin(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 ignore.');
            return;
        }
        for (const r of results) {
            if (r.status === 'applied') {
                this.log(`${chalk.cyan('[IGNORED]')} ${r.path}`);
            }
            else {
                this.log(`${chalk.dim('[NOOP]')} ${r.path} — ${r.reason ?? 'no change'}`);
            }
        }
    }
}
