/**
 * DevExtreme (esm/__internal/ui/html_editor/converters/m_markdown.js)
 * Version: 24.1.7-build-24272-1937
 * Build date: Sat Sep 28 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import {
    getWindow
} from "../../../../core/utils/window";
import Errors from "../../../../ui/widget/ui.errors";
import ShowDown from "showdown";
import TurnDown from "turndown";
import converterController from "../m_converterController";
class MarkdownConverter {
    constructor() {
        var _this$_html2Markdown;
        const window = getWindow();
        const turndown = window && window.TurndownService || TurnDown;
        const showdown = window && window.showdown || ShowDown;
        if (!turndown) {
            throw Errors.Error("E1041", "Turndown")
        }
        if (!showdown) {
            throw Errors.Error("E1041", "Showdown")
        }
        this._html2Markdown = new turndown;
        if (null !== (_this$_html2Markdown = this._html2Markdown) && void 0 !== _this$_html2Markdown && _this$_html2Markdown.addRule) {
            this._html2Markdown.addRule("emptyLine", {
                filter: element => "p" === element.nodeName.toLowerCase() && "<br>" === element.innerHTML,
                replacement: () => "<br>"
            });
            this._html2Markdown.keep(["table"])
        }
        this._markdown2Html = new showdown.Converter({
            simpleLineBreaks: true,
            strikethrough: true,
            tables: true
        })
    }
    toMarkdown(htmlMarkup) {
        return this._html2Markdown.turndown(htmlMarkup || "")
    }
    toHtml(markdownMarkup) {
        let markup = this._markdown2Html.makeHtml(markdownMarkup);
        if (markup) {
            markup = markup.replace(new RegExp("\\r?\\n", "g"), "")
        }
        return markup
    }
}
converterController.addConverter("markdown", MarkdownConverter);
export default MarkdownConverter;
