"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseServiceArgs = parseServiceArgs;
const service_1 = require("../../commands/service");
const catalog_1 = require("../../i18n/catalog");
const shared_1 = require("./shared");
function parseServiceArgs(args, options) {
    const locale = options?.locale ?? catalog_1.DEFAULT_APP_LOCALE;
    const [action, ...rest] = args;
    if (!action) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.service.usage", {
            usage: service_1.SERVICE_COMMAND_USAGE,
        }));
    }
    if (action === "log") {
        return parseServiceLogArgs(rest, locale);
    }
    const lifecycleOptions = parseLifecycleOptions(action, rest, locale);
    if (action === "enable") {
        return lifecycleOptions.refreshEnv
            ? { action, refreshEnv: true }
            : { action };
    }
    if (action === "disable") {
        return lifecycleOptions.force ? { action, force: true } : { action };
    }
    if (action === "restart") {
        return {
            action,
            ...(lifecycleOptions.force ? { force: true } : {}),
            ...(lifecycleOptions.refreshEnv ? { refreshEnv: true } : {}),
        };
    }
    if (options?.allowInternal && (0, service_1.isServiceCommandAction)(action)) {
        return { action };
    }
    if ((0, service_1.isPublicServiceCommandAction)(action)) {
        return { action };
    }
    throw new Error((0, catalog_1.translate)(locale, "cli.parser.service.unknownCommand", { action }));
}
function parseLifecycleOptions(action, args, locale) {
    let force = false;
    let refreshEnv = false;
    for (const token of args) {
        if ((action === "disable" || action === "restart") && token === "--force") {
            if (force) {
                throwUnknownServiceArgument(token, locale);
            }
            force = true;
            continue;
        }
        if ((action === "enable" || action === "restart") &&
            token === "--refresh-env") {
            if (refreshEnv) {
                throwUnknownServiceArgument(token, locale);
            }
            refreshEnv = true;
            continue;
        }
        throwUnknownServiceArgument(token ?? "", locale);
    }
    return { force, refreshEnv };
}
function throwUnknownServiceArgument(token, locale) {
    throw new Error((0, catalog_1.translate)(locale, "cli.parser.service.unknownArgument", {
        token,
    }));
}
function parseServiceLogArgs(args, locale) {
    const [action, ...rest] = args;
    if (!action) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.service.log.usage"));
    }
    if (action !== "show") {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.service.log.unknownCommand", { action }));
    }
    const values = {};
    for (let index = 0; index < rest.length; index += 1) {
        const token = rest[index];
        const value = rest[index + 1];
        switch (token) {
            case "--tail":
                values.tail = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parsePositiveIntegerArg)("--tail", optionValue, locale));
                index += 1;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.service.log.show.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    return values.tail === undefined
        ? {
            action: "log-show",
        }
        : {
            action: "log-show",
            tail: values.tail,
        };
}
