"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.shorthandsByCommandName = exports.getCommandFullName = exports.getRCOptionsTypes = exports.getCliOptionsTypes = exports.GLOBAL_OPTIONS = void 0;
const config_1 = require("@pnpm/config");
const plugin_commands_audit_1 = require("@pnpm/plugin-commands-audit");
const plugin_commands_import_1 = require("@pnpm/plugin-commands-import");
const plugin_commands_installation_1 = require("@pnpm/plugin-commands-installation");
const plugin_commands_listing_1 = require("@pnpm/plugin-commands-listing");
const plugin_commands_outdated_1 = require("@pnpm/plugin-commands-outdated");
const plugin_commands_publishing_1 = require("@pnpm/plugin-commands-publishing");
const plugin_commands_rebuild_1 = require("@pnpm/plugin-commands-rebuild");
const plugin_commands_script_runners_1 = require("@pnpm/plugin-commands-script-runners");
const plugin_commands_server_1 = require("@pnpm/plugin-commands-server");
const plugin_commands_store_1 = require("@pnpm/plugin-commands-store");
const R = require("ramda");
const bin = require("./bin");
const completion_1 = require("./completion");
const help_1 = require("./help");
const installTest = require("./installTest");
const recursive = require("./recursive");
const root = require("./root");
exports.GLOBAL_OPTIONS = R.pick([
    'color',
    'dir',
    'filter',
    'loglevel',
    'help',
    'parseable',
    'prefix',
    'reporter',
    'stream',
    'workspace-packages',
], config_1.types);
const commands = [
    plugin_commands_installation_1.add,
    plugin_commands_audit_1.audit,
    bin,
    plugin_commands_script_runners_1.exec,
    plugin_commands_import_1.importCommand,
    plugin_commands_installation_1.install,
    installTest,
    plugin_commands_installation_1.link,
    plugin_commands_listing_1.list,
    plugin_commands_outdated_1.outdated,
    plugin_commands_publishing_1.pack,
    plugin_commands_installation_1.prune,
    plugin_commands_publishing_1.publish,
    plugin_commands_rebuild_1.rebuild,
    recursive,
    plugin_commands_installation_1.remove,
    plugin_commands_script_runners_1.restart,
    root,
    plugin_commands_script_runners_1.run,
    plugin_commands_server_1.server,
    plugin_commands_script_runners_1.start,
    plugin_commands_script_runners_1.stop,
    plugin_commands_store_1.store,
    plugin_commands_script_runners_1.test,
    plugin_commands_installation_1.unlink,
    plugin_commands_installation_1.update,
    plugin_commands_listing_1.why,
];
const handlerByCommandName = {};
const helpByCommandName = {};
const cliOptionsTypesByCommandName = {};
const rcOptionsTypesByCommandName = {};
const aliasToFullName = new Map();
const completionByCommandName = {};
const shorthandsByCommandName = {};
exports.shorthandsByCommandName = shorthandsByCommandName;
for (let i = 0; i < commands.length; i++) {
    const { cliOptionsTypes, commandNames, completion, handler, help, rcOptionsTypes, shorthands, } = commands[i];
    if (!commandNames || commandNames.length === 0) {
        throw new Error('The command at index ' + i + " doesn't have command names");
    }
    for (const commandName of commandNames) {
        handlerByCommandName[commandName] = handler;
        helpByCommandName[commandName] = help;
        cliOptionsTypesByCommandName[commandName] = cliOptionsTypes;
        rcOptionsTypesByCommandName[commandName] = rcOptionsTypes;
        shorthandsByCommandName[commandName] = shorthands !== null && shorthands !== void 0 ? shorthands : {};
        if (completion) {
            completionByCommandName[commandName] = completion;
        }
    }
    if (commandNames.length > 1) {
        const fullName = commandNames[0];
        for (let i = 1; i < commandNames.length; i++) {
            aliasToFullName.set(commandNames[i], fullName);
        }
    }
}
handlerByCommandName.help = help_1.default(helpByCommandName);
handlerByCommandName.completion = completion_1.default({
    cliOptionsTypesByCommandName,
    completionByCommandName,
    initialCompletion,
    shorthandsByCommandName,
    universalOptionsTypes: exports.GLOBAL_OPTIONS,
});
function initialCompletion() {
    return Object.keys(handlerByCommandName).map((name) => ({ name }));
}
exports.default = handlerByCommandName;
function getCliOptionsTypes(commandName) {
    var _a;
    return ((_a = cliOptionsTypesByCommandName[commandName]) === null || _a === void 0 ? void 0 : _a.call(cliOptionsTypesByCommandName)) || {};
}
exports.getCliOptionsTypes = getCliOptionsTypes;
function getRCOptionsTypes(commandName) {
    var _a;
    return (commandName && ((_a = rcOptionsTypesByCommandName[commandName]) === null || _a === void 0 ? void 0 : _a.call(rcOptionsTypesByCommandName))) || {};
}
exports.getRCOptionsTypes = getRCOptionsTypes;
function getCommandFullName(commandName) {
    return aliasToFullName.get(commandName) || commandName;
}
exports.getCommandFullName = getCommandFullName;
