/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var React = require('react');
var markdownIt = require('markdown-it');
var markdownItHtml5Media = require('markdown-it-html5-media');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var markdownIt__default = /*#__PURE__*/_interopDefaultLegacy(markdownIt);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var doMarkdown = markdownIt__default["default"]();
doMarkdown.use(markdownItHtml5Media.html5Media);
function markdown(content, options) {
    if (options) {
        doMarkdown.set(options);
    }
    return doMarkdown.render(content);
}
var Markdown = /** @class */ (function (_super) {
    tslib.__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 tslib.__awaiter(this, void 0, void 0, function () {
            var _a, content, options;
            return tslib.__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 (_J$X_("div", { "data-testid": "markdown-body", className: "markdown-body", ref: this.htmlRef }));
    };
    Markdown.defaultProps = {
        content: '',
        options: {
            linkify: true
        }
    };
    return Markdown;
}(React__default["default"].Component));

exports["default"] = Markdown;
exports.markdown = markdown;
