"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseInitArgs = parseInitArgs;
const control_plane_config_1 = require("../../config/control-plane-config");
const catalog_1 = require("../../i18n/catalog");
const display_time_1 = require("../../i18n/display-time");
const shared_1 = require("./shared");
function parseInitArgs(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 "--create-repo":
                values.createRepo = true;
                break;
            case "--repo-provider":
                values.repositoryHostKind = (0, shared_1.requireOptionValue)(token, value, locale);
                index += 1;
                break;
            case "--repo":
                values.repositoryHostProjectRef = (0, shared_1.requireOptionValue)(token, value, locale);
                index += 1;
                break;
            case "--base-branch":
                values.repositoryHostBaseBranch = (0, shared_1.requireOptionValue)(token, value, locale);
                index += 1;
                break;
            case "--remote-name":
                values.repositoryHostRemoteName = (0, shared_1.requireOptionValue)(token, value, locale);
                index += 1;
                break;
            case "--tracker-provider":
                values.ticketTrackerKind = (0, shared_1.requireOptionValue)(token, value, locale);
                index += 1;
                break;
            case "--tracker":
                values.ticketTrackerProjectRef = (0, shared_1.requireOptionValue)(token, value, locale);
                index += 1;
                break;
            case "--poll-interval":
                values.pollIntervalSec = Number((0, shared_1.requireOptionValue)(token, value, locale));
                index += 1;
                break;
            case "--max-workers":
                values.maxWorkers = (0, shared_1.parsePositiveIntegerArg)(token, (0, shared_1.requireOptionValue)(token, value, locale), locale);
                index += 1;
                break;
            case "--max-issues":
                values.maxIssues = (0, shared_1.parsePositiveIntegerArg)(token, (0, shared_1.requireOptionValue)(token, value, locale), locale);
                index += 1;
                break;
            case "--parallelism-mode":
                values.parallelismMode = parseParallelismModeArg((0, shared_1.requireOptionValue)(token, value, locale), locale);
                index += 1;
                break;
            case "--max-ai-runs":
                values.aiRunLimitsMaxRuns = (0, shared_1.parseNonNegativeIntegerArg)(token, (0, shared_1.requireOptionValue)(token, value, locale), locale);
                index += 1;
                break;
            case "--max-ai-hours": {
                const rawMaxHours = (0, shared_1.requireOptionValue)(token, value, locale);
                values.aiRunLimitsMaxHours =
                    rawMaxHours === "off"
                        ? null
                        : (0, shared_1.parseNonNegativeNumberArg)(token, rawMaxHours, locale);
                index += 1;
                break;
            }
            case "--reviewer":
                values.reviewers ??= [];
                values.reviewers.push((0, shared_1.requireOptionValue)(token, value, locale));
                index += 1;
                break;
            case "--language":
            case "--display-time-zone":
                parseInitI18nArg(values, token, (0, shared_1.requireOptionValue)(token, value, locale), locale);
                index += 1;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.init.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    return values;
}
function parseInitI18nArg(values, token, value, locale) {
    switch (token) {
        case "--language":
            values.language = parseLanguageArg(value, locale);
            break;
        case "--display-time-zone":
            values.displayTimeZone = parseDisplayTimeZoneArg(value, locale);
            break;
    }
}
function parseParallelismModeArg(value, locale) {
    if (control_plane_config_1.PARALLELISM_MODE_VALUES.includes(value)) {
        return value;
    }
    throw new Error((0, catalog_1.translate)(locale, "cli.parser.init.invalidParallelismMode", { value }));
}
function parseLanguageArg(value, locale) {
    if ((0, catalog_1.isAppLocale)(value)) {
        return value;
    }
    throw new Error((0, catalog_1.translate)(locale, "cli.parser.init.invalidLanguage", { value }));
}
function parseDisplayTimeZoneArg(value, locale) {
    if ((0, display_time_1.isSupportedTimeZone)(value)) {
        return value;
    }
    throw new Error((0, catalog_1.translate)(locale, "config.error.displayTimeZoneInvalid"));
}
