"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerStatusCommand = registerStatusCommand;
const daemon_discovery_js_1 = require("../../lib/daemon-discovery.js");
const ui_js_1 = require("../ui.js");
function registerStatusCommand(program) {
    program
        .command('status')
        .description('Check daemon health')
        .action(async () => {
        try {
            const daemonUrl = await (0, daemon_discovery_js_1.resolveDaemonUrl)();
            const cockpitUrl = await (0, daemon_discovery_js_1.resolveCockpitUrl)();
            const response = await fetch(`${daemonUrl}/api/v1/health`);
            if (!response.ok) {
                console.log('');
                console.log((0, ui_js_1.header)(ui_js_1.sym.err, 'Daemon is not running', 'start with `chorus start`'));
                console.log('');
                process.exit(1);
            }
            const envelope = (await response.json());
            const data = envelope.data;
            if (envelope.ok && data) {
                const uptime = Math.floor(data.uptime / 1000);
                const human = uptime < 60
                    ? `${uptime}s`
                    : uptime < 3600
                        ? `${Math.floor(uptime / 60)}m`
                        : `${Math.floor(uptime / 3600)}h ${Math.floor((uptime % 3600) / 60)}m`;
                console.log('');
                console.log((0, ui_js_1.header)(ui_js_1.sym.ok, 'Chorus is running'));
                console.log('');
                console.log((0, ui_js_1.kv)([
                    ['Version', ui_js_1.c.cyan(data.version)],
                    ['Uptime', ui_js_1.c.dim(human)],
                    ['Cockpit', ui_js_1.c.cyan(cockpitUrl)],
                    ['Daemon', ui_js_1.c.dim(daemonUrl)],
                ]));
                console.log('');
            }
            else {
                console.log('');
                console.log((0, ui_js_1.header)(ui_js_1.sym.err, 'Daemon is not responding correctly'));
                console.log('');
                process.exit(1);
            }
        }
        catch {
            console.log('');
            console.log((0, ui_js_1.header)(ui_js_1.sym.err, 'Daemon is not running', 'start with `chorus start`'));
            console.log('');
            process.exit(1);
        }
    });
}
//# sourceMappingURL=status.js.map