/**
 * DevExtreme (cjs/__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/
 */
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.default = void 0;
var _window = require("../../../../core/utils/window");
var _ui = _interopRequireDefault(require("../../../../ui/widget/ui.errors"));
var _showdown = _interopRequireDefault(require("showdown"));
var _turndown = _interopRequireDefault(require("turndown"));
var _m_converterController = _interopRequireDefault(require("../m_converterController"));

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}
class MarkdownConverter {
    constructor() {
        var _this$_html2Markdown;
        const window = (0, _window.getWindow)();
        const turndown = window && window.TurndownService || _turndown.default;
        const showdown = window && window.showdown || _showdown.default;
        if (!turndown) {
            throw _ui.default.Error("E1041", "Turndown")
        }
        if (!showdown) {
            throw _ui.default.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
    }
}
_m_converterController.default.addConverter("markdown", MarkdownConverter);
var _default = exports.default = MarkdownConverter;
