const SUBCOMMANDS_THAT_KEEP_ARGV = new Set([
    "pixel",
    "mcp",
    "login",
    "logout",
    "telegram",
    "doctor",
    "agent-home-login",
]);
function isCliSubcommandName(value) {
    return (value === "pixel" ||
        value === "mcp" ||
        value === "login" ||
        value === "logout" ||
        value === "sessions" ||
        value === "telegram" ||
        value === "serve" ||
        value === "doctor" ||
        value === "agent-home-login" ||
        value === "agent-home");
}
function stripSubcommandArg(argv) {
    argv.splice(2, 1);
}
/**
 * Routes the early CLI command layer before parseArgs handles normal flags.
 *
 * This intentionally preserves the legacy startup contract:
 * - any -h/--help anywhere prints top-level help and exits before subcommands;
 * - selected subcommands remove their command token before their mode parser runs;
 * - `continue` is not a standalone handler, it only toggles the later TUI resume path.
 */
export function routeCliCommandInput(options) {
    const { argv, printHelp, exit, handlers } = options;
    if (argv.includes("--help") || argv.includes("-h")) {
        printHelp();
        exit(0);
    }
    const subcommand = argv[2];
    if (isCliSubcommandName(subcommand)) {
        if (!SUBCOMMANDS_THAT_KEEP_ARGV.has(subcommand)) {
            stripSubcommandArg(argv);
        }
        handlers[subcommand]();
        return { kind: "handled", subcommand };
    }
    if (subcommand === "continue") {
        stripSubcommandArg(argv);
        return { kind: "continue" };
    }
    return { kind: "main", subcommand };
}
//# sourceMappingURL=command-routing.js.map