"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseLabelsArgs = parseLabelsArgs;
exports.parseRunArgs = parseRunArgs;
exports.parsePublishArgs = parsePublishArgs;
exports.parseDetachArgs = parseDetachArgs;
exports.parseCleanupArgs = parseCleanupArgs;
const catalog_1 = require("../../i18n/catalog");
const login_support_1 = require("../../platform/repository-host/login-support");
const shared_1 = require("./shared");
function parseLabelsArgs(args, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const [action, ...rest] = args;
    if (!action) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.labels.usage"));
    }
    if (action === "sync") {
        if (rest.length > 0) {
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.labels.unknownArgument", {
                token: rest[0] ?? "",
            }));
        }
        return { action };
    }
    throw new Error((0, catalog_1.translate)(locale, "cli.parser.labels.unknownCommand", { action }));
}
function parseRunArgs(args, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const values = {};
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--issue":
                values.issueIds = (0, shared_1.parseIssueIds)(token, value, locale);
                index += 1;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.run.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    return values;
}
function parsePublishArgs(args, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const values = {};
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        switch (token) {
            case "-m":
            case "--message": {
                const value = args[index + 1];
                if (!value) {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.publish.missingMessage"));
                }
                values.message = value;
                index += 1;
                break;
            }
            case "--path": {
                const value = args[index + 1];
                if (!value) {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.publish.missingPath"));
                }
                values.paths = [...(values.paths ?? []), value];
                index += 1;
                break;
            }
            case "--reviewer": {
                const value = args[index + 1];
                if (!value) {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.publish.missingReviewer"));
                }
                const reviewers = (0, login_support_1.normalizeLoginListValues)([value]);
                if (reviewers.length === 0) {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.publish.missingReviewer"));
                }
                values.reviewers = (0, login_support_1.normalizeLoginListValues)([
                    ...(values.reviewers ?? []),
                    ...reviewers,
                ]);
                index += 1;
                break;
            }
            case "-y":
            case "--yes":
                values.yes = true;
                break;
            case "--no-merge":
                values.noMerge = true;
                break;
            case "--restart-service":
                values.restartService = true;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.publish.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    return values;
}
function parseDetachArgs(args, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const values = {};
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--cwd":
                values.cwd = (0, shared_1.requireOptionValue)(token, value, locale);
                index += 1;
                break;
            case "--purge":
                values.purge = true;
                break;
            case "--force":
                values.force = true;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.detach.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    return values;
}
function parseCleanupArgs(args, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const values = {};
    for (const token of args) {
        switch (token) {
            case "--workspaces":
                values.workspaces = true;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.cleanup.unknownOption", { token }));
        }
    }
    return values;
}
