"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.dispatchNamedCommand = exports.resolveNamedCommand = void 0;
const DEFAULT_HELP_TOKENS = ['help', '--help', '-h'];
function resolveNamedCommand(token, routes) {
    if (!token) {
        return undefined;
    }
    return routes.find((route) => route.name === token || route.aliases?.includes(token));
}
exports.resolveNamedCommand = resolveNamedCommand;
async function dispatchNamedCommand(options) {
    const { args, routes, onUnknown, onHelp, allowEmptyHelp = false } = options;
    const helpTokens = options.helpTokens || DEFAULT_HELP_TOKENS;
    const command = args[0];
    if (!command) {
        if (!allowEmptyHelp || !onHelp) {
            return false;
        }
        await onHelp();
        return true;
    }
    if (helpTokens.includes(command)) {
        if (!onHelp) {
            return false;
        }
        await onHelp();
        return true;
    }
    const route = resolveNamedCommand(command, routes);
    if (!route) {
        await onUnknown(command);
        return true;
    }
    await route.handle(args.slice(1));
    return true;
}
exports.dispatchNamedCommand = dispatchNamedCommand;
//# sourceMappingURL=named-command-router.js.map