"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runDocsCommand = runDocsCommand;
const catalog_1 = require("../docs/catalog");
const catalog_2 = require("../i18n/catalog");
const docs_web_1 = require("./docs-web");
async function runDocsCommand(options, locale) {
    const docs = await (0, catalog_1.loadDocsCatalog)();
    switch (options.action) {
        case "list":
            console.log(options.withDescription
                ? (0, catalog_1.renderDocsIndex)(docs, locale)
                : (0, catalog_1.renderDocsPathList)(docs, locale));
            return 0;
        case "show": {
            const doc = (0, catalog_1.findDocTopic)(docs, options.topic);
            if (doc === null) {
                console.error((0, catalog_2.translate)(locale, "docs.command.unknownTopic", {
                    topic: options.topic,
                }));
                console.log((0, catalog_1.renderDocsIndex)(docs, locale));
                return 1;
            }
            if (options.web) {
                const url = await (0, docs_web_1.openDocsTopicWeb)(doc);
                console.log((0, catalog_2.translate)(locale, "docs.command.webOpened", { url }));
                return 0;
            }
            console.log((0, catalog_1.renderDocTopic)(doc));
            return 0;
        }
        case "search": {
            const results = (0, catalog_1.searchDocs)(docs, options.query, options.view === "headings" ? 0 : options.context);
            console.log(options.view === "headings"
                ? (0, catalog_1.renderDocsSearchHeadingResults)(results, locale)
                : (0, catalog_1.renderDocsSearchResults)(results, locale));
            return 0;
        }
    }
}
