"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const fs_1 = __importDefault(require("fs"));
const open_browser_js_1 = require("./open-browser.js");
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const daemon_discovery_js_1 = require("../lib/daemon-discovery.js");
const audit_js_1 = require("./commands/audit.js");
const diagnose_js_1 = require("./commands/diagnose.js");
const doctor_js_1 = require("./commands/doctor.js");
const init_js_1 = require("./commands/init.js");
const quickstart_js_1 = require("./commands/quickstart.js");
const start_js_1 = require("./commands/start.js");
const status_js_1 = require("./commands/status.js");
const stop_js_1 = require("./commands/stop.js");
const update_js_1 = require("./commands/update.js");
const runtime_env_js_1 = require("./runtime-env.js");
const shared_js_1 = require("./shared.js");
const ui_js_1 = require("./ui.js");
const program = new commander_1.Command();
program
    .name('chorus')
    .description('Driver-agnostic multi-LLM peer review for code decisions')
    .version(shared_js_1.pkg.version);
// Show a quick-start banner before the standard help so first-time
// users see the setup sequence even when npm's global install hides
// postinstall stdout. State-aware: chorus.db is the marker — dir alone
// is not enough, an empty ~/.chorus can exist from a prior aborted
// install.
program.addHelpText('beforeAll', () => {
    const chorusDir = path_1.default.join(os_1.default.homedir(), '.chorus');
    const dbFile = path_1.default.join(chorusDir, 'chorus.db');
    const daemonPid = path_1.default.join(chorusDir, 'daemon.pid');
    const initialised = fs_1.default.existsSync(dbFile);
    const running = fs_1.default.existsSync(daemonPid);
    if (!initialised) {
        return [
            '',
            `  ${ui_js_1.sym.rocket} ${ui_js_1.c.bold('Welcome to Chorus')} ${ui_js_1.c.dim('— two commands to get going:')}`,
            '',
            `    ${ui_js_1.c.cyan('1.')} ${ui_js_1.c.bold('chorus init')}     ${ui_js_1.c.dim('register MCP with your editors + seed templates + detect CLIs')}`,
            `    ${ui_js_1.c.cyan('2.')} ${ui_js_1.c.bold('chorus start')}    ${ui_js_1.c.dim('bring up the daemon + cockpit')}`,
            '',
        ].join('\n');
    }
    if (!running) {
        return [
            '',
            `  ${ui_js_1.sym.pointer} ${ui_js_1.c.bold('Daemon is stopped.')} ${ui_js_1.c.dim('Bring it back up:')}`,
            '',
            `    ${ui_js_1.c.bold('chorus start')}`,
            '',
        ].join('\n');
    }
    return '';
});
(0, init_js_1.registerInitCommand)(program);
(0, start_js_1.registerStartCommand)(program);
(0, stop_js_1.registerStopCommand)(program);
(0, status_js_1.registerStatusCommand)(program);
(0, doctor_js_1.registerDoctorCommand)(program);
(0, diagnose_js_1.registerDiagnoseCommand)(program);
(0, update_js_1.registerUpdateCommand)(program);
(0, quickstart_js_1.registerQuickstartCommand)(program);
(0, audit_js_1.registerAuditCommand)(program);
program
    .command('ui')
    .description('Open the Chorus web UI in default browser')
    .action(async () => {
    try {
        const env = (0, runtime_env_js_1.detectRuntimeEnv)();
        const cockpitUrl = await (0, daemon_discovery_js_1.resolveCockpitUrl)();
        console.log('');
        console.log(`   ${ui_js_1.c.gray('Open')}  ${ui_js_1.c.cyan(cockpitUrl)}`);
        if (env.hint) {
            console.log('');
            console.log((0, ui_js_1.tip)(env.hint));
        }
        console.log('');
        if ((0, runtime_env_js_1.shouldAutoOpenBrowser)(env)) {
            await (0, open_browser_js_1.openBrowser)(cockpitUrl);
            console.log(`\nOpening ${cockpitUrl}...`);
        }
    }
    catch (error) {
        console.error('Failed to open browser:', error);
        process.exit(1);
    }
});
program
    .command('connect [orchestrator]')
    .description('Pre-approve all Chorus MCP tools in your orchestrator (default: claude)')
    .action(async (orchestrator) => {
    const { runConnect } = await import('./connect.js');
    runConnect(orchestrator);
});
program
    .command('mcp')
    .description('Run the MCP server on stdio (for orchestrators)')
    .action(async () => {
    // Hand off stdio to the MCP server. This call never returns under
    // normal operation — the orchestrator (Claude Code, Codex, Cursor)
    // holds the pipe open and pumps JSON-RPC messages until it shuts
    // the child down.
    await import('../mcp/index.js');
});
program.parse(process.argv);
if (!process.argv.slice(2).length) {
    program.outputHelp();
}
//# sourceMappingURL=index.js.map