"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseCapabilityArgs = parseCapabilityArgs;
const control_plane_config_1 = require("../../config/control-plane-config");
const catalog_1 = require("../../i18n/catalog");
const shared_1 = require("./shared");
function parseCapabilityArgs(args, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const [action, ...rest] = args;
    if (!action) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.capability.usage"));
    }
    switch (action) {
        case "list":
            return parseCapabilityListArgs(rest, locale);
        case "search":
            return parseCapabilitySearchArgs(rest, locale);
        default:
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.capability.unknownCommand", { action }));
    }
}
function parseCapabilityListArgs(args, locale) {
    let target;
    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) => parseCapabilityWorkerOrAllTarget((0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale), locale));
                index += 1;
                break;
            case "--include-excluded":
                includeExcluded = true;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.capability.list.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    return {
        action: "list",
        ...(includeExcluded ? { includeExcluded } : {}),
        ...(target === undefined ? {} : { target }),
    };
}
function parseCapabilitySearchArgs(args, locale) {
    let query;
    let target;
    let mode = "all";
    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) => parseCapabilityWorkerOrAllTarget((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.capability.search.modeConflict"));
                }
                mode = "installed";
                break;
            case "--installable-only":
                if (mode !== "all") {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.capability.search.modeConflict"));
                }
                mode = "installable";
                break;
            case "--include-excluded":
                includeExcluded = true;
                break;
            default:
                if (token?.startsWith("-")) {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.capability.search.unknownOption", {
                        token: token ?? "",
                    }));
                }
                if (query !== undefined) {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.capability.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.capability.search.missingQuery"));
    }
    return {
        action: "search",
        query,
        mode,
        ...(includeExcluded ? { includeExcluded } : {}),
        ...(target === undefined ? {} : { target }),
    };
}
function parseCapabilityWorkerOrAllTarget(value, locale) {
    if (value === "all") {
        return value;
    }
    if ((0, control_plane_config_1.isBuiltInWorkerName)(value)) {
        return value;
    }
    const parsed = control_plane_config_1.customWorkerIdSchema.safeParse(value);
    if (parsed.success) {
        return parsed.data;
    }
    throw new Error((0, catalog_1.translate)(locale, "cli.parser.capability.invalidTarget", { value }));
}
