"use strict";
const path_1 = require("path");
const lineNumbers = require("@vuepress/markdown/lib/lineNumbers");
const code_demo_1 = require("./markdown-it/code-demo");
const flowchart_1 = require("./markdown-it/flowchart");
const footnote_1 = require("./markdown-it/footnote");
const katex_1 = require("./markdown-it/katex");
const mark_1 = require("./markdown-it/mark");
const presentation_1 = require("./markdown-it/presentation");
const sub_1 = require("./markdown-it/sub");
const sup_1 = require("./markdown-it/sup");
const pluginConfig_1 = require("./pluginConfig");
module.exports = (option, { themeConfig }) => {
    const markdownOption = Object.keys(option).length === 0 ? themeConfig.mdEnhance || {} : option;
    return {
        name: "md-enhance",
        define: () => ({
            MARKDOWN_ENHANCE_OPTIONS: markdownOption,
            CODE_DEMO_OPTIONS: {
                ...code_demo_1.codeDemoDefaultSetting,
                ...(typeof markdownOption.codeDemo === "boolean"
                    ? {}
                    : markdownOption.codeDemo),
            },
            REVEAL_PLUGINS: typeof markdownOption.presentation === "object" &&
                Array.isArray(markdownOption.presentation.plugins)
                ? markdownOption.presentation.plugins
                : [],
            REVEAL_CONFIG: typeof markdownOption.presentation === "object" &&
                typeof markdownOption.presentation.revealConfig === "object"
                ? markdownOption.presentation.revealConfig
                : {},
        }),
        enhanceAppFiles: path_1.resolve(__dirname, "../client/enhanceAppFile.js"),
        clientRootMixin: path_1.resolve(__dirname, "../client/clientRootMixin.js"),
        chainMarkdown: (md) => {
            if (markdownOption.lineNumbers !== false)
                md.plugin("line-numbers").use(lineNumbers);
            if (markdownOption.sup || markdownOption.enableAll)
                md.plugin("sup").use(sup_1.default);
            if (markdownOption.sub || markdownOption.enableAll)
                md.plugin("sub").use(sub_1.default);
            if (markdownOption.footnote || markdownOption.enableAll)
                md.plugin("footnote").use(footnote_1.default);
            if (markdownOption.mark || markdownOption.enableAll)
                md.plugin("mark").use(mark_1.default);
            if (markdownOption.flowchart || markdownOption.enableAll)
                md.plugin("flowchart").use(flowchart_1.default);
            if (markdownOption.tex || markdownOption.enableAll)
                md.plugin("katex").use(katex_1.default, [
                    {
                        macros: {
                            // support more symbols
                            "\\liiiint": "\\int\\!\\!\\!\\iiint",
                            "\\iiiint": "\\int\\!\\!\\!\\!\\iiint",
                            "\\idotsint": "\\int\\!\\cdots\\!\\int",
                        },
                    },
                ]);
            if (markdownOption.presentation || markdownOption.enableAll)
                md.plugin("presentation").use(presentation_1.default);
        },
        plugins: pluginConfig_1.default(markdownOption, themeConfig),
    };
};
