"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.latexPastePlugin = void 0;
const prosemirror_state_1 = require("prosemirror-state");
const _core_1 = require("../../../core/index.js");
const model_1 = require("../../../pm/model.js");
const const_1 = require("./const.js");
const utils_1 = require("./utils.js");
const latexPastePlugin = () => new prosemirror_state_1.Plugin({
    props: {
        handleDOMEvents: {
            paste(view, e) {
                const event = e;
                if (!event.clipboardData || view.state.selection.$from.parent.type.spec.code) {
                    return false;
                }
                const latexData = (0, utils_1.getLatexData)(event.clipboardData);
                if (!latexData)
                    return false;
                (0, _core_1.getLoggerFromState)(view.state).event({
                    domEvent: 'paste',
                    event: 'paste-latex-from-code-editor',
                    editor: latexData.editor,
                    editorMode: latexData.mode,
                    empty: !latexData.value,
                    dataTypes: event.clipboardData.types,
                });
                const { tr, schema } = view.state;
                const mathBlockType = schema.nodes[const_1.MathNode.Block];
                if (!mathBlockType)
                    return false;
                if (latexData.value) {
                    const formulas = (0, utils_1.parseLatexFormulas)(latexData.value);
                    if (formulas.length > 0) {
                        const nodes = formulas.map((formula) => mathBlockType.create(null, schema.text(formula)));
                        const fragment = model_1.Fragment.from(nodes);
                        tr.replaceWith(tr.selection.from, tr.selection.to, fragment);
                    }
                    else {
                        tr.replaceWith(tr.selection.from, tr.selection.to, model_1.Fragment.empty);
                    }
                }
                else {
                    tr.replaceWith(tr.selection.from, tr.selection.to, model_1.Fragment.empty);
                }
                view.dispatch(tr.scrollIntoView());
                e.preventDefault();
                return true;
            },
        },
    },
});
exports.latexPastePlugin = latexPastePlugin;
//# sourceMappingURL=latex-paste-plugin.js.map
