"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseMcpArgs = parseMcpArgs;
const control_plane_config_1 = require("../../config/control-plane-config");
const catalog_1 = require("../../i18n/catalog");
const shared_1 = require("./shared");
function parseMcpArgs(args, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const [action, ...rest] = args;
    if (!action) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.usage"));
    }
    switch (action) {
        case "list":
            return parseMcpListArgs(rest, locale);
        case "search":
            return parseMcpSearchArgs(rest, locale);
        case "install":
            return parseMcpInstallArgs(rest, locale);
        case "create":
            return parseMcpCreateArgs(rest, locale);
        case "verify":
            return parseMcpVerifyArgs(rest, locale);
        case "catalog":
            return parseMcpCatalogArgs(rest, locale);
        default:
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.unknownCommand", { action }));
    }
}
function parseMcpListArgs(args, locale) {
    const values = { action: "list" };
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--for":
                values.target = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => parseMcpWorkerOrAllTarget((0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale), locale));
                index += 1;
                break;
            case "--include-excluded":
                values.includeExcluded = true;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.list.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    return values;
}
function parseMcpSearchArgs(args, locale) {
    let query;
    let target;
    let mode = "all";
    let withWorker = false;
    let includeExcluded = false;
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--for":
                target = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => parseMcpWorkerOrAllTarget((0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale), locale));
                index += 1;
                break;
            case "--installed-only":
                if (mode !== "all") {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.search.modeConflict"));
                }
                mode = "installed";
                break;
            case "--installable-only":
                if (mode !== "all") {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.search.modeConflict"));
                }
                mode = "installable";
                break;
            case "--with-worker":
                withWorker = true;
                break;
            case "--include-excluded":
                includeExcluded = true;
                break;
            default:
                if (token?.startsWith("-")) {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.search.unknownOption", {
                        token: token ?? "",
                    }));
                }
                if (query !== undefined) {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.search.unexpectedArgument", {
                        value: token ?? "",
                    }));
                }
                query = (0, shared_1.parseNonEmptyStringArg)("query", token ?? "", locale);
                break;
        }
    }
    if (query === undefined) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.search.missingQuery"));
    }
    return {
        action: "search",
        query,
        mode,
        ...(includeExcluded ? { includeExcluded } : {}),
        ...(withWorker ? { withWorker } : {}),
        ...(target === undefined ? {} : { target }),
    };
}
function parseMcpInstallArgs(args, locale) {
    let id;
    let query;
    let target;
    let serverName;
    let authProfileId;
    let overwrite = false;
    let includeExcluded = false;
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--id":
                id = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--search":
                query = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--for":
                target = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => parseMcpWorkerTarget((0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale), locale));
                index += 1;
                break;
            case "--name":
                serverName = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--profile":
                authProfileId = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--overwrite":
                overwrite = true;
                break;
            case "--include-excluded":
                includeExcluded = true;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.install.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    if ((id === undefined) === (query === undefined)) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.install.sourceMode"));
    }
    if (target === undefined) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.install.missingTarget"));
    }
    return {
        action: "install",
        ...(id === undefined ? {} : { id }),
        ...(query === undefined ? {} : { query }),
        target,
        ...(serverName === undefined ? {} : { serverName }),
        ...(authProfileId === undefined ? {} : { authProfileId }),
        overwrite,
        ...(includeExcluded ? { includeExcluded } : {}),
    };
}
function parseMcpCreateArgs(args, locale) {
    let prompt;
    let applyDraftPath;
    let target = "all";
    let targetSpecified = false;
    let saveDraft = false;
    for (let index = 0; index < args.length;) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--prompt":
                prompt = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyFreeformTextArg)(token, optionValue, locale));
                index += 2;
                break;
            case "--apply-draft":
                applyDraftPath = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 2;
                break;
            case "--for":
                if (targetSpecified) {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.create.duplicateTarget"));
                }
                target = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => parseMcpWorkerOrAllTarget((0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale), locale));
                targetSpecified = true;
                index += 2;
                break;
            case "--save-draft":
                saveDraft = true;
                index += 1;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.create.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    if (applyDraftPath !== undefined) {
        if (prompt !== undefined || targetSpecified || saveDraft) {
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.create.applyDraftMode"));
        }
        return {
            action: "create",
            mode: "apply-draft",
            applyDraftPath,
        };
    }
    if (prompt === undefined) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.create.missingPrompt"));
    }
    return {
        action: "create",
        mode: "prompt",
        prompt,
        target,
        ...(saveDraft ? { saveDraft: true } : {}),
    };
}
function parseMcpVerifyArgs(args, locale) {
    let target;
    const refs = [];
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--for":
                target = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => parseMcpWorkerOrAllTarget((0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale), locale));
                index += 1;
                break;
            case "--ref":
                refs.push((0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale)));
                index += 1;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.verify.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    return {
        action: "verify",
        refs,
        ...(target === undefined ? {} : { target }),
    };
}
function parseMcpCatalogArgs(args, locale) {
    const [catalogAction, ...rest] = args;
    if (!catalogAction) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.catalog.usage"));
    }
    switch (catalogAction) {
        case "list":
            return parseMcpCatalogListArgs(rest, locale);
        case "add":
            return parseMcpCatalogAddArgs(rest, locale);
        case "remove":
            return parseMcpCatalogRemoveArgs(rest, locale);
        default:
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.catalog.unknownCommand", {
                action: catalogAction,
            }));
    }
}
function parseMcpCatalogListArgs(args, locale) {
    let includeSystem = false;
    for (const token of args) {
        switch (token) {
            case "--include-system":
                includeSystem = true;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.catalog.list.unknownOption", {
                    token,
                }));
        }
    }
    return { action: "catalog", catalogAction: "list", includeSystem };
}
function parseMcpCatalogAddArgs(args, locale) {
    let entryPath;
    let overwrite = false;
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--entry":
                entryPath = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--overwrite":
                overwrite = true;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.catalog.add.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    if (entryPath === undefined) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.catalog.add.usage"));
    }
    return {
        action: "catalog",
        catalogAction: "add",
        entryPath,
        overwrite,
    };
}
function parseMcpCatalogRemoveArgs(args, locale) {
    let id;
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--id":
                id = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.catalog.remove.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    if (id === undefined) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.catalog.remove.usage"));
    }
    return { action: "catalog", catalogAction: "remove", id };
}
function parseMcpWorkerOrAllTarget(value, locale) {
    if (value === "all") {
        return value;
    }
    return parseMcpWorkerTarget(value, locale);
}
function parseMcpWorkerTarget(value, locale) {
    if (value !== "all" &&
        value !== "default" &&
        ((0, control_plane_config_1.isBuiltInWorkerName)(value) ||
            control_plane_config_1.customWorkerIdSchema.safeParse(value).success)) {
        return value;
    }
    throw new Error((0, catalog_1.translate)(locale, "cli.parser.mcp.invalidTarget", { value }));
}
