import { Args, Command, Flags } from '@oclif/core';
import chalk from 'chalk';
import { acceptOrigin } from '../../origin/domain/actions.js';
import { discoverOrigins } from '../../origin/domain/origin-file.js';
export default class OriginAccept extends Command {
    static args = {
        pathspec: Args.string({
            description: 'Optional file or directory. Defaults to recursive cwd scan.',
            required: false,
        }),
    };
    static description = 'Adopt the codegen proposal: replace current with `.origin` and delete `.origin`.';
    static examples = [
        '<%= config.bin %> origin accept',
        '<%= config.bin %> origin accept backend/foo.ts',
        '<%= config.bin %> origin accept backend/iam/',
        '<%= config.bin %> origin accept --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(OriginAccept);
        const origins = discoverOrigins(args.pathspec);
        if (origins.length === 0 && args.pathspec) {
            // Single-file pathspec with no origin → still emit one noop entry so
            // callers get a predictable response shape.
            const results = [acceptOrigin(args.pathspec)];
            this.emit(results, flags.json);
            return;
        }
        const results = origins.map((entry) => acceptOrigin(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 accept.');
            return;
        }
        for (const r of results) {
            if (r.status === 'applied') {
                this.log(`${chalk.green('[ACCEPTED]')} ${r.path}`);
            }
            else {
                this.log(`${chalk.dim('[NOOP]')} ${r.path} — ${r.reason ?? 'no change'}`);
            }
        }
    }
}
