'use strict';

var remarkMath = require('remark-math');

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

function _interopNamespace(e) {
    if (e && e.__esModule) return e;
    var n = Object.create(null);
    if (e) {
        Object.keys(e).forEach(function (k) {
            if (k !== 'default') {
                var d = Object.getOwnPropertyDescriptor(e, k);
                Object.defineProperty(n, k, d.get ? d : {
                    enumerable: true,
                    get: function () {
                        return e[k];
                    }
                });
            }
        });
    }
    n['default'] = e;
    return Object.freeze(n);
}

var remarkMath__default = /*#__PURE__*/_interopDefaultLegacy(remarkMath);

// DO NOT EDIT, generated by scripts/icon.js
const icons = {
    math: '<svg width="1em" height="1em" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="m40 9-3-3H8l18 18L8 42h29l3-3" stroke="currentColor" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/></svg>',
    inline: '<svg width="1em" height="1em" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="m37 9-3-3H8l17 18L8 42h26l3-3M5 24h10M33 24h10" stroke="currentColor" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/></svg>',
    block: '<svg width="1em" height="1em" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="m36 11-3-3H12l16 16-16 16h21l3-3M6 5v38M42 5v38" stroke="currentColor" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/></svg>',
};

var inline = "Inline formula";
var inlineText = "formula";
var block = "Block formula";
var blockText = "formula";
var en = {
	inline: inline,
	inlineText: inlineText,
	block: block,
	blockText: blockText
};

function math({ locale: _locale, katexOptions, } = {}) {
    const locale = { ...en, ..._locale };
    let katex;
    return {
        remark: (p) => p.use(remarkMath__default['default']),
        viewerEffect({ markdownBody }) {
            const renderMath = async (selector, displayMode) => {
                const els = markdownBody.querySelectorAll(selector);
                if (els.length === 0)
                    return;
                if (!katex) {
                    katex = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('katex')); });
                }
                els.forEach((el) => {
                    katex.render(el.innerText, el, {
                        ...katexOptions,
                        throwOnError: false,
                        displayMode,
                    });
                });
            };
            renderMath('.math.math-inline', false);
            renderMath('.math.math-display', true);
        },
        actions: [
            {
                icon: icons.math,
                handler: {
                    type: 'dropdown',
                    actions: [
                        {
                            title: locale.inline,
                            icon: icons.inline,
                            cheatsheet: `$${locale.inlineText}$`,
                            handler: {
                                type: 'action',
                                click({ wrapText, editor }) {
                                    wrapText('$');
                                    editor.focus();
                                },
                            },
                        },
                        {
                            title: locale.block,
                            icon: icons.block,
                            cheatsheet: `$$↵${locale.blockText}↵$$`,
                            handler: {
                                type: 'action',
                                click({ appendBlock, editor, codemirror }) {
                                    const { line } = appendBlock('$$\n\\TeX\n$$');
                                    editor.setSelection(codemirror.Pos(line + 1, 0), codemirror.Pos(line + 1, 4));
                                    editor.focus();
                                },
                            },
                        },
                    ],
                },
            },
        ],
    };
}

module.exports = math;
//# sourceMappingURL=index.cjs.js.map
