"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.detectCommand = void 0;
const chalk_1 = __importDefault(require("chalk"));
const helpers_1 = require("./helpers");
const context_1 = require("./context");
exports.detectCommand = {
    command: 'detect',
    describe: 'Detect the framework in the current project (outputs JSON)',
    builder: (yargs) => yargs.options({
        'install-dir': {
            describe: 'project directory to inspect',
            type: 'string',
        },
    }),
    handler: (argv) => {
        void (async () => {
            const installDir = argv['install-dir'] ?? process.cwd();
            const { resolveMode } = await import('../lib/mode-config.js');
            const { jsonOutput } = resolveMode({
                json: argv.json,
                human: argv.human,
                isTTY: Boolean(process.stdout.isTTY),
            });
            try {
                const { runDetect } = await import('../lib/agent-ops.js');
                const result = await runDetect(installDir);
                if (jsonOutput) {
                    process.stdout.write(JSON.stringify(result) + '\n');
                }
                else if (result.integration) {
                    (0, helpers_1.getUI)().log.success(`Detected ${chalk_1.default.bold(result.frameworkName ?? result.integration)} (${result.integration})`);
                }
                else {
                    (0, helpers_1.getUI)().note(`No framework detected. Run \`${context_1.CLI_INVOCATION} --menu\` to pick one manually.`);
                }
                process.exit(result.integration ? 0 : 1);
            }
            catch (e) {
                const message = e instanceof Error ? e.message : String(e);
                if (jsonOutput) {
                    process.stdout.write(JSON.stringify({ error: message }) + '\n');
                }
                else {
                    (0, helpers_1.getUI)().log.error(`Detection failed: ${message}`);
                }
                process.exit(helpers_1.ExitCode.GENERAL_ERROR);
            }
        })();
    },
};
