"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseAuthArgs = parseAuthArgs;
const catalog_1 = require("../../i18n/catalog");
const shared_1 = require("./shared");
function parseAuthArgs(args, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const [action, ...rest] = args;
    if (!action) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.auth.usage"));
    }
    if (isZeroArgumentAuthAction(action)) {
        if (rest.length > 0) {
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.auth.unknownArgument", {
                token: rest[0] ?? "",
            }));
        }
        return { action };
    }
    switch (action) {
        case "clear":
            return parseAuthClearArgs(rest, locale);
        default:
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.auth.unknownCommand", { action }));
    }
}
function isZeroArgumentAuthAction(action) {
    return action === "setup" || action === "list" || action === "order";
}
function parseAuthClearArgs(args, locale) {
    let all = false;
    let targetProvider;
    let targetProviderSpecified = false;
    let invalid = false;
    let yes = false;
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--all":
                all = true;
                break;
            case "--for":
                if (targetProviderSpecified) {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.auth.clear.duplicateFor"));
                }
                targetProvider = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => parseAuthClearTargetProvider((0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale), locale));
                targetProviderSpecified = true;
                index += 1;
                break;
            case "--invalid":
                invalid = true;
                break;
            case "-y":
            case "--yes":
                yes = true;
                break;
            default:
                if (token?.startsWith("-")) {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.auth.clear.unknownOption", {
                        token: token ?? "",
                    }));
                }
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.auth.clear.unknownArgument", {
                    token: token ?? "",
                }));
        }
    }
    if (all && (targetProvider !== undefined || invalid)) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.auth.clear.allConflict"));
    }
    return {
        action: "clear",
        all,
        ...(targetProvider === undefined ? {} : { targetProvider }),
        invalid,
        yes,
    };
}
function parseAuthClearTargetProvider(value, locale) {
    if (value === "codex" || value === "claude") {
        return value;
    }
    throw new Error((0, catalog_1.translate)(locale, "cli.parser.auth.clear.invalidFor", { value }));
}
