#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseWorkflowArgs = exports.parseStateArgs = exports.parseSkillArgs = exports.parseServiceArgs = exports.parseRunArgs = exports.parsePublishArgs = exports.parseMcpArgs = exports.parseLabelsArgs = exports.parseIssueBroadcastArgs = exports.parseIssueArgs = exports.parseInitArgs = exports.parseEventHookArgs = exports.parseEjectArgs = exports.parseDocsArgs = exports.parseDetachArgs = exports.parseCleanupArgs = exports.parseCatalogArgs = exports.parseCapabilityArgs = exports.parseAuthArgs = void 0;
exports.main = main;
const auth_1 = require("../commands/auth");
const capability_1 = require("../commands/capability");
const catalog_1 = require("../commands/catalog");
const cleanup_1 = require("../commands/cleanup");
const detach_1 = require("../commands/detach");
const doctor_1 = require("../commands/doctor");
const docs_1 = require("../commands/docs");
const eject_1 = require("../commands/eject");
const event_hook_1 = require("../commands/event-hook");
const init_1 = require("../commands/init");
const issue_1 = require("../commands/issue");
const labels_1 = require("../commands/labels");
const locale_1 = require("../commands/locale");
const mcp_1 = require("../commands/mcp");
const publish_1 = require("../commands/publish");
const run_1 = require("../commands/run");
const service_1 = require("../commands/service");
const skill_1 = require("../commands/skill");
const state_1 = require("../commands/state");
const workflow_1 = require("../commands/workflow");
const branding_1 = require("../core/branding");
const catalog_2 = require("../i18n/catalog");
const arg_parsers_1 = require("./arg-parsers");
const help_1 = require("./help");
async function main(argv = process.argv.slice(2), cwd = process.cwd(), env = process.env) {
    const locale = await (0, locale_1.loadCommandLocale)(cwd);
    const helpRequest = (0, help_1.resolveHelpRequest)(argv);
    if (helpRequest !== undefined) {
        if (helpRequest.kind === "unsupported-command-help") {
            console.error((0, catalog_2.translate)(locale, "cli.helpCommand.unsupported", {
                command: helpRequest.path.join(" "),
            }));
            console.error((0, catalog_2.translate)(locale, "cli.helpCommand.commandHelpHint", {
                commandHelp: `${branding_1.CLI_COMMAND_NAME} ${helpRequest.path.join(" ")} --help`,
            }));
            console.error((0, catalog_2.translate)(locale, "cli.helpCommand.docsHint"));
            return 1;
        }
        const printed = await (0, help_1.printHelp)(cwd, helpRequest.path, helpRequest.all);
        if (printed) {
            return 0;
        }
        console.error((0, catalog_2.translate)(locale, "cli.command.unknown", {
            command: helpRequest.path.join(" "),
        }));
        await (0, help_1.printNearestHelp)(cwd, helpRequest.path);
        return 1;
    }
    const [command = "run", ...args] = argv;
    let exitCode = 0;
    switch (command) {
        case "init":
            exitCode = await (0, init_1.runInitCommand)(cwd, (0, arg_parsers_1.parseInitArgs)(args, locale));
            break;
        case "doctor":
            exitCode = await (0, doctor_1.runDoctorCommand)(cwd);
            break;
        case "docs":
            exitCode = await (0, docs_1.runDocsCommand)((0, arg_parsers_1.parseDocsArgs)(args, locale), locale);
            break;
        case "eject":
            exitCode = await (0, eject_1.runEjectCommand)(cwd, (0, arg_parsers_1.parseEjectArgs)(args, locale));
            break;
        case "event-hook":
            exitCode = await (0, event_hook_1.runEventHookCommand)(cwd, (0, arg_parsers_1.parseEventHookArgs)(args, locale));
            break;
        case "auth":
            exitCode = await (0, auth_1.runAuthCommand)((0, arg_parsers_1.parseAuthArgs)(args, locale));
            break;
        case "capability":
            exitCode = await (0, capability_1.runCapabilityCommand)(cwd, (0, arg_parsers_1.parseCapabilityArgs)(args, locale));
            break;
        case "catalog":
            exitCode = await (0, catalog_1.runCatalogCommand)(cwd, (0, arg_parsers_1.parseCatalogArgs)(args, locale));
            break;
        case "detach":
            exitCode = await (0, detach_1.runDetachCommand)(cwd, (0, arg_parsers_1.parseDetachArgs)(args, locale));
            break;
        case "cleanup":
            exitCode = await (0, cleanup_1.runCleanupCommand)({
                cwd,
                ...(0, arg_parsers_1.parseCleanupArgs)(args, locale),
            });
            break;
        case "run":
            exitCode = await (0, run_1.runQueueCommand)(cwd, (0, arg_parsers_1.parseRunArgs)(args, locale));
            break;
        case "publish":
            exitCode = await (0, publish_1.runPublishCommand)(cwd, (0, arg_parsers_1.parsePublishArgs)(args, locale));
            break;
        case "issue":
            exitCode = await (0, issue_1.runIssueCommand)(cwd, (0, arg_parsers_1.parseIssueArgs)(args, locale));
            break;
        case "labels":
            exitCode = await (0, labels_1.runLabelsCommand)(cwd, (0, arg_parsers_1.parseLabelsArgs)(args, locale));
            break;
        case "mcp":
            exitCode = await (0, mcp_1.runMcpCommand)(cwd, (0, arg_parsers_1.parseMcpArgs)(args, locale));
            break;
        case "service":
            exitCode = await (0, service_1.runServiceCommand)((0, arg_parsers_1.parseServiceArgs)(args, {
                allowInternal: env[branding_1.INTERNAL_SERVICE_ENV_NAME] === "1",
                locale,
            }));
            break;
        case "skill":
            exitCode = await (0, skill_1.runSkillCommand)(cwd, (0, arg_parsers_1.parseSkillArgs)(args, locale));
            break;
        case "state":
            exitCode = await (0, state_1.runStateCommand)(cwd, (0, arg_parsers_1.parseStateArgs)(args, locale));
            break;
        case "workflow":
            exitCode = await (0, workflow_1.runWorkflowCommand)(cwd, (0, arg_parsers_1.parseWorkflowArgs)(args, locale), locale);
            break;
        default:
            console.error((0, catalog_2.translate)(locale, "cli.command.unknown", {
                command,
            }));
            await (0, help_1.printHelp)(cwd);
            exitCode = 1;
    }
    return exitCode;
}
var arg_parsers_2 = require("./arg-parsers");
Object.defineProperty(exports, "parseAuthArgs", { enumerable: true, get: function () { return arg_parsers_2.parseAuthArgs; } });
Object.defineProperty(exports, "parseCapabilityArgs", { enumerable: true, get: function () { return arg_parsers_2.parseCapabilityArgs; } });
Object.defineProperty(exports, "parseCatalogArgs", { enumerable: true, get: function () { return arg_parsers_2.parseCatalogArgs; } });
Object.defineProperty(exports, "parseCleanupArgs", { enumerable: true, get: function () { return arg_parsers_2.parseCleanupArgs; } });
Object.defineProperty(exports, "parseDetachArgs", { enumerable: true, get: function () { return arg_parsers_2.parseDetachArgs; } });
Object.defineProperty(exports, "parseDocsArgs", { enumerable: true, get: function () { return arg_parsers_2.parseDocsArgs; } });
Object.defineProperty(exports, "parseEjectArgs", { enumerable: true, get: function () { return arg_parsers_2.parseEjectArgs; } });
Object.defineProperty(exports, "parseEventHookArgs", { enumerable: true, get: function () { return arg_parsers_2.parseEventHookArgs; } });
Object.defineProperty(exports, "parseInitArgs", { enumerable: true, get: function () { return arg_parsers_2.parseInitArgs; } });
Object.defineProperty(exports, "parseIssueArgs", { enumerable: true, get: function () { return arg_parsers_2.parseIssueArgs; } });
Object.defineProperty(exports, "parseIssueBroadcastArgs", { enumerable: true, get: function () { return arg_parsers_2.parseIssueBroadcastArgs; } });
Object.defineProperty(exports, "parseLabelsArgs", { enumerable: true, get: function () { return arg_parsers_2.parseLabelsArgs; } });
Object.defineProperty(exports, "parseMcpArgs", { enumerable: true, get: function () { return arg_parsers_2.parseMcpArgs; } });
Object.defineProperty(exports, "parsePublishArgs", { enumerable: true, get: function () { return arg_parsers_2.parsePublishArgs; } });
Object.defineProperty(exports, "parseRunArgs", { enumerable: true, get: function () { return arg_parsers_2.parseRunArgs; } });
Object.defineProperty(exports, "parseServiceArgs", { enumerable: true, get: function () { return arg_parsers_2.parseServiceArgs; } });
Object.defineProperty(exports, "parseSkillArgs", { enumerable: true, get: function () { return arg_parsers_2.parseSkillArgs; } });
Object.defineProperty(exports, "parseStateArgs", { enumerable: true, get: function () { return arg_parsers_2.parseStateArgs; } });
Object.defineProperty(exports, "parseWorkflowArgs", { enumerable: true, get: function () { return arg_parsers_2.parseWorkflowArgs; } });
if (require.main === module) {
    void main()
        .then((exitCode) => {
        process.exitCode = exitCode;
    })
        .catch(async (error) => {
        const locale = await (0, locale_1.loadRawCommandLocale)(process.cwd()).catch(() => catalog_2.DEFAULT_APP_LOCALE);
        const message = error instanceof Error
            ? error.message
            : (0, catalog_2.translate)(locale, "cli.command.unknownError");
        console.error(message);
        process.exitCode = 1;
    });
}
