/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

import { __extends, __awaiter, __generator } from 'tslib';
import React__default from 'react';
import markdownIt from 'markdown-it';
import { html5Media } from 'markdown-it-html5-media';

var doMarkdown = markdownIt();
doMarkdown.use(html5Media);
function markdown(content, options) {
    if (options) {
        doMarkdown.set(options);
    }
    return doMarkdown.render(content);
}
var Markdown = /** @class */ (function (_super) {
    __extends(Markdown, _super);
    function Markdown(props) {
        var _this = _super.call(this, props) || this;
        _this.htmlRef = _this.htmlRef.bind(_this);
        return _this;
    }
    Markdown.prototype.htmlRef = function (dom) {
        this.dom = dom;
        if (!dom) {
            return;
        }
        this._render();
    };
    Markdown.prototype.componentDidUpdate = function (nextProps) {
        if (this.props.content !== nextProps.content) {
            this._render();
        }
    };
    Markdown.prototype._render = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _a, content, options;
            return __generator(this, function (_b) {
                _a = this.props, content = _a.content, options = _a.options;
                this.dom.innerHTML = markdown(content, options);
                // @ts-ignore 需要用户手动加载 katex
                if (typeof renderMathInElement === 'function') {
                    // @ts-ignore
                    renderMathInElement(this.dom, {
                        delimiters: [
                            { left: '$$', right: '$$', display: true },
                            { left: '$', right: '$', display: false }
                        ]
                    });
                }
                return [2 /*return*/];
            });
        });
    };
    Markdown.prototype.render = function () {
        return (React__default.createElement("div", { "data-testid": "markdown-body", className: "markdown-body", ref: this.htmlRef }));
    };
    Markdown.defaultProps = {
        content: '',
        options: {
            linkify: true
        }
    };
    return Markdown;
}(React__default.Component));

export { Markdown as default, markdown };
