import * as __WEBPACK_EXTERNAL_MODULE_mermaid__ from "mermaid";
function mermaidPlugin(md, options) {
    __WEBPACK_EXTERNAL_MODULE_mermaid__["default"].initialize({
        securityLevel: 'loose',
        ...options
    });
    function getLangName(info) {
        return info.split(/\s+/g)[0];
    }
    const defaultFenceRenderer = md.renderer.rules.fence;
    function customFenceRenderer(tokens, idx, options, env, slf) {
        const token = tokens[idx];
        const info = token.info.trim();
        const langName = info ? getLangName(info) : '';
        if (-1 === [
            'mermaid',
            '{mermaid}'
        ].indexOf(langName)) {
            if (void 0 !== defaultFenceRenderer) return defaultFenceRenderer(tokens, idx, options, env, slf);
            return '';
        }
        let imageHTML = '';
        const imageAttrs = [];
        const element = document.createElement('div');
        document.body.appendChild(element);
        try {
            const container_id = 'mermaid-container';
            __WEBPACK_EXTERNAL_MODULE_mermaid__["default"].mermaidAPI.render(container_id, token.content, (html)=>{
                const svg = document.getElementById(container_id);
                if (null !== svg) imageAttrs.push([
                    'style',
                    "max-width:".concat(svg.style.maxWidth, ";max-height:").concat(svg.style.maxHeight)
                ]);
                imageHTML = html;
            }, element);
        } catch (e) {
            return '<div class="alert alert-danger">'.concat(e, "</div>");
        } finally{
            element.remove();
        }
        imageAttrs.push([
            'src',
            "data:image/svg+xml,".concat(encodeURIComponent(imageHTML))
        ]);
        return "<img ".concat(slf.renderAttrs({
            attrs: imageAttrs
        }), ">");
    }
    md.renderer.rules.fence = customFenceRenderer;
}
export { mermaidPlugin as default };
