"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseStateArgs = parseStateArgs;
const catalog_1 = require("../../i18n/catalog");
const shared_1 = require("./shared");
function parseStateArgs(args, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const [action, ...rest] = args;
    if (!action) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.state.usage"));
    }
    switch (action) {
        case "patch":
            return parseStatePatchArgs(rest, locale);
        case "restore":
            return parseStateRestoreArgs(rest, locale);
        default:
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.state.unknownCommand", { action }));
    }
}
function parseStatePatchArgs(args, locale) {
    const values = {
        action: "patch",
        dryRun: false,
    };
    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;
            case "--state":
                values.state = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseIssueState)(optionValue, locale));
                index += 1;
                break;
            case "--dry-run":
                values.dryRun = true;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.state.patch.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    return values;
}
function parseStateRestoreArgs(args, locale) {
    const values = {
        dryRun: false,
        force: false,
    };
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--backup-file":
                values.backupFilePath = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--issue":
                values.issueIds = (0, shared_1.parseIssueIds)(token, value, locale);
                index += 1;
                break;
            case "--dry-run":
                values.dryRun = true;
                break;
            case "--force":
                values.force = true;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.state.restore.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    return {
        action: "restore",
        backupFilePath: (0, shared_1.requireParsedOption)(values.backupFilePath, "--backup-file", locale),
        ...(values.issueIds !== undefined ? { issueIds: values.issueIds } : {}),
        dryRun: values.dryRun,
        force: values.force,
    };
}
