function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _array_without_holes(arr) {
    if (Array.isArray(arr)) return _array_like_to_array(arr);
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _non_iterable_spread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _to_array(arr) {
    return _array_with_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_rest();
}
function _to_consumable_array(arr) {
    return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import rehypeKatex from "rehype-katex";
import rehypeRaw from "rehype-raw";
import remarkFrontmatter from "remark-frontmatter";
import remarkGfm from "remark-gfm";
import remarkMath from "remark-math";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import { unified } from "unified";
import { remarkDirectiveContainer } from "../MarkdownEditor/editor/parser/remarkDirectiveContainer";
import remarkDirectiveContainersOnly from "../MarkdownEditor/editor/parser/remarkDirectiveContainersOnly";
import { convertParagraphToImage, fixStrongWithSpecialChars, protectJinjaDollarInText } from "../MarkdownEditor/editor/parser/remarkParse";
import { REMARK_REHYPE_DIRECTIVE_HANDLERS } from "../MarkdownEditor/editor/utils/markdownToHtml";
import { rehypeSanitizeUserHtml } from "../Utils/rehypeSanitizeUserHtml";
import { rehypeFootnoteRef } from "./plugins/rehypeFootnoteRef";
import { remarkChartFromComment } from "./plugins/remarkChartFromComment";
/** `remark-math` 启用 `$...$` 单美元符号包裹的行内数学 */ var INLINE_MATH_WITH_SINGLE_DOLLAR = {
    singleDollarTextMath: true
};
/** `remark-frontmatter` 仅识别 yaml 风格 frontmatter */ var FRONTMATTER_LANGUAGES = [
    'yaml'
];
/** `remark-directive-container` 默认渲染配置 */ var REMARK_DIRECTIVE_CONTAINER_OPTIONS = {
    className: 'markdown-container',
    titleElement: {
        className: [
            'markdown-container__title'
        ]
    }
};
var remarkRehypePlugin = remarkRehype;
/**
 * 构建 markdown → mdast → hast 的 unified 处理器。
 *
 * 内置插件链（顺序敏感）：
 * 1. `remarkParse` - markdown 字符串 → mdast
 * 2. `remarkGfm` - GFM 扩展（表格、删除线、任务列表、脚注）
 * 3. `fixStrongWithSpecialChars` - 修复 `**xxx**` 内含特殊字符的解析
 * 4. `convertParagraphToImage` - 段落仅含图片时升级为图片块
 * 5. `protectJinjaDollarInText` - 保护 Jinja 模板中的 `$` 不被 math 误识别
 * 6. `remarkMath` - 行内/块级数学公式识别
 * 7. `remarkFrontmatter` - yaml frontmatter 识别
 * 8. `remarkDirectiveContainersOnly` + `remarkDirectiveContainer` - `:::` 容器指令
 * 9. `remarkChartFromComment` - HTML 注释 + 表格 → chart 代码块
 * 10. `remarkRehype` - mdast → hast（保留原始 HTML）
 * 11. `rehypeRaw` - 处理 hast 中的原始 HTML 字符串
 * 12. `rehypeSanitizeUserHtml` - 安全过滤用户 HTML
 * 13. `rehypeKatex` - 数学公式 → KaTeX
 * 14. `rehypeFootnoteRef` - 裸 `[^N]` → fnc span（无底部定义场景）
 *
 * 之后追加用户自定义的 `extraRemarkPlugins` 与 `config.markedConfig`。
 */ export var createHastProcessor = function createHastProcessor(extraRemarkPlugins, config) {
    var processor = unified();
    processor.use(remarkParse).use(remarkGfm, {
        singleTilde: false
    }).use(fixStrongWithSpecialChars).use(convertParagraphToImage).use(protectJinjaDollarInText).use(remarkMath, INLINE_MATH_WITH_SINGLE_DOLLAR).use(remarkFrontmatter, FRONTMATTER_LANGUAGES).use(remarkDirectiveContainersOnly).use(remarkDirectiveContainer, REMARK_DIRECTIVE_CONTAINER_OPTIONS).use(remarkChartFromComment).use(remarkRehypePlugin, {
        allowDangerousHtml: true,
        handlers: REMARK_REHYPE_DIRECTIVE_HANDLERS
    }).use(rehypeRaw).use(rehypeSanitizeUserHtml).use(rehypeKatex, {
        strict: 'ignore'
    }).use(rehypeFootnoteRef);
    if (extraRemarkPlugins) {
        extraRemarkPlugins.forEach(function(entry) {
            if (Array.isArray(entry)) {
                var _processor;
                var _entry = _to_array(entry), plugin = _entry[0], pluginOptions = _entry.slice(1);
                (_processor = processor).use.apply(_processor, [
                    plugin
                ].concat(_to_consumable_array(pluginOptions)));
            } else {
                processor.use(entry);
            }
        });
    }
    if (config === null || config === void 0 ? void 0 : config.markedConfig) {
        config.markedConfig.forEach(function(entry) {
            if (Array.isArray(entry)) {
                var _processor;
                var _entry = _to_array(entry), plugin = _entry[0], pluginOptions = _entry.slice(1);
                (_processor = processor).use.apply(_processor, [
                    plugin
                ].concat(_to_consumable_array(pluginOptions)));
            } else {
                processor.use(entry);
            }
        });
    }
    return processor;
};
