import { COMMAND_ALIASES, COMMANDS, } from './command-registry.js';
export function isCommandName(value) {
    return Object.prototype.hasOwnProperty.call(COMMANDS, value);
}
export function normalizeCommand(args = []) {
    const cmd = args[0];
    if (!cmd)
        return { command: null, args: [...args] };
    let mapped = cmd in COMMAND_ALIASES ? COMMAND_ALIASES[cmd] : cmd;
    const rest = args.slice(1);
    return {
        command: isCommandName(mapped) ? mapped : null,
        args: rest,
    };
}
export async function dispatch(args) {
    const argv = args ?? process.argv.slice(2);
    const { command, args: rest } = normalizeCommand(argv);
    if (!command) {
        if (!argv.length) {
            const mod = await import('../commands/doctor.js');
            return mod.run('doctor', []);
        }
        const raw = argv[0] ?? '';
        console.error(`Unknown command: ${raw}`);
        process.exitCode = 1;
        const result = {
            ok: false,
            status: 'blocked',
            command: raw,
            reason: 'unknown_command',
        };
        return result;
    }
    const entry = COMMANDS[command];
    const mod = await entry.lazy();
    if (typeof mod.run !== 'function')
        throw new Error(`Command ${command} must export run(command, args)`);
    return mod.run(command, rest);
}
//# sourceMappingURL=router.js.map