"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseDocsArgs = parseDocsArgs;
const catalog_1 = require("../../i18n/catalog");
const constants_1 = require("../../docs/constants");
const shared_1 = require("./shared");
function parseDocsArgs(args, locale) {
    const [action, ...rest] = args;
    if (action === undefined) {
        return { action: "list", withDescription: true };
    }
    if (action === "list") {
        return parseDocsListArgs(rest, locale);
    }
    if (action === "search") {
        return parseDocsSearchArgs(rest, locale);
    }
    if (action === "show") {
        return parseDocsShowArgs(rest, locale);
    }
    if (action.startsWith("-")) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.docs.unknownOption", { token: action }));
    }
    throw new Error((0, catalog_1.translate)(locale, "cli.parser.docs.unknownCommand", { command: action }));
}
function parseDocsListArgs(args, locale) {
    let withDescription = false;
    for (const token of args) {
        switch (token) {
            case "--with-description":
                withDescription = true;
                break;
            default:
                if (token === undefined) {
                    continue;
                }
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.docs.list.unknownOption", { token }));
        }
    }
    return { action: "list", withDescription };
}
function parseDocsShowArgs(args, locale) {
    let topic;
    let web = false;
    for (const token of args) {
        switch (token) {
            case "--web":
                web = true;
                break;
            default:
                if (token === undefined) {
                    continue;
                }
                if (token.startsWith("-") || topic !== undefined) {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.docs.show.unknownOption", {
                        token,
                    }));
                }
                topic = token;
                break;
        }
    }
    if (topic === undefined || topic.length === 0) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.docs.show.usage"));
    }
    return {
        action: "show",
        topic: (0, shared_1.parseNonEmptyStringArg)("topic", topic, locale),
        web,
    };
}
function parseDocsSearchArgs(args, locale) {
    const queryParts = [];
    let context = constants_1.DEFAULT_DOCS_SEARCH_CONTEXT;
    let view = "matches";
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--context":
                context = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonNegativeIntegerArg)("--context", optionValue, locale));
                index += 1;
                break;
            case "--view":
                view = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => parseDocsSearchView(optionValue, locale));
                index += 1;
                break;
            default:
                if (token === undefined) {
                    continue;
                }
                if (token.startsWith("-")) {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.docs.search.unknownOption", {
                        token,
                    }));
                }
                queryParts.push(token);
        }
    }
    if (queryParts.length === 0) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.docs.search.usage"));
    }
    return {
        action: "search",
        query: (0, shared_1.parseNonEmptyStringArg)("query", queryParts.join(" "), locale),
        context,
        view,
    };
}
function parseDocsSearchView(value, locale) {
    if (value === "matches" || value === "headings") {
        return value;
    }
    throw new Error((0, catalog_1.translate)(locale, "cli.parser.invalidViewValue", { value }));
}
