"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeBlockHighlight = void 0;
const prosemirror_state_1 = require("prosemirror-state");
// @ts-ignore // TODO: fix cjs build
const prosemirror_utils_1 = require("prosemirror-utils");
const prosemirror_view_1 = require("prosemirror-view");
const lodash_1 = require("../../../../lodash.js");
const logger_1 = require("../../../../logger.js");
const CodeBlockSpecs_1 = require("../CodeBlockSpecs/index.js");
const TooltipPlugin_1 = require("./TooltipPlugin/index.js");
const key = new prosemirror_state_1.PluginKey('code_block_highlight');
const CodeBlockHighlight = (builder, opts) => {
    let langs;
    let lowlight;
    let hljs;
    const loadModules = async () => {
        try {
            hljs = (await import('highlight.js/lib/core')).default;
            const low = await import('lowlight');
            const all = low.all;
            const create = low.createLowlight;
            langs = { ...all, ...opts.langs };
            lowlight = create(langs);
            return true;
        }
        catch (e) {
            logger_1.globalLogger.info('Skip code_block highlighting');
            builder.logger.log('Skip code_block highlighting');
            return false;
        }
    };
    builder.addPlugin(() => {
        let modulesLoaded = false;
        const selectItems = [];
        const mapping = {};
        // TODO: add TAB key handler
        // TODO: Remove constant selection of block
        return new prosemirror_state_1.Plugin({
            key,
            state: {
                init: (_, state) => {
                    loadModules().then((loaded) => {
                        modulesLoaded = loaded;
                        if (modulesLoaded) {
                            for (const lang of Object.keys(langs)) {
                                const defs = langs[lang](hljs);
                                selectItems.push({
                                    value: lang,
                                    content: defs.name || (0, lodash_1.capitalize)(lang),
                                });
                                if (defs.aliases) {
                                    for (const alias of defs.aliases) {
                                        mapping[alias] = lang;
                                    }
                                }
                            }
                        }
                    });
                    return getDecorations(state.doc);
                },
                apply: (tr, decos, oldState, newState) => {
                    if (!modulesLoaded) {
                        return prosemirror_view_1.DecorationSet.empty;
                    }
                    if (tr.docChanged) {
                        const oldNodeName = oldState.selection.$head.parent.type.name;
                        const newNodeName = newState.selection.$head.parent.type.name;
                        // Apply decorations if:
                        if (oldNodeName === CodeBlockSpecs_1.codeBlockNodeName ||
                            newNodeName === CodeBlockSpecs_1.codeBlockNodeName) {
                            // selection includes codeblock node,
                            return getDecorations(tr.doc);
                        }
                        else {
                            const oldNodes = (0, prosemirror_utils_1.findChildrenByType)(oldState.doc, (0, CodeBlockSpecs_1.codeBlockType)(oldState.schema));
                            const newNodes = (0, prosemirror_utils_1.findChildrenByType)(newState.doc, (0, CodeBlockSpecs_1.codeBlockType)(newState.schema));
                            if (
                            // OR transaction adds/removes codeblock nodes,
                            newNodes.length !== oldNodes.length ||
                                // OR transaction has changes that completely encapsulate a node
                                // (for example, a transaction that affects the entire document).
                                // Such transactions can happen during collab syncing via y-prosemirror, for example.
                                tr.steps.some((step) => {
                                    return (stepHasFromTo(step) &&
                                        oldNodes.some((node) => node.pos >= step.from &&
                                            node.pos + node.node.nodeSize <= step.to));
                                })) {
                                return getDecorations(tr.doc);
                            }
                        }
                    }
                    return decos.map(tr.mapping, tr.doc);
                },
            },
            view: (view) => (0, TooltipPlugin_1.codeLangSelectTooltipViewCreator)(view, selectItems, mapping),
            props: {
                decorations: (state) => {
                    return key.getState(state);
                },
                nodeViews: {
                    [CodeBlockSpecs_1.codeBlockNodeName]: (node) => {
                        let prevLang = node.attrs[CodeBlockSpecs_1.CodeBlockNodeAttr.Lang];
                        const dom = document.createElement('pre');
                        const contentDOM = document.createElement('code');
                        contentDOM.classList.add('hljs');
                        if (prevLang) {
                            dom.setAttribute(CodeBlockSpecs_1.CodeBlockNodeAttr.Lang, prevLang);
                            contentDOM.classList.add(prevLang);
                        }
                        dom.append(contentDOM);
                        return {
                            dom,
                            contentDOM,
                            update(newNode) {
                                if (node.type !== newNode.type)
                                    return false;
                                const newLang = newNode.attrs[CodeBlockSpecs_1.CodeBlockNodeAttr.Lang];
                                if (prevLang !== newLang) {
                                    contentDOM.className = 'hljs';
                                    if (newLang) {
                                        dom.setAttribute(CodeBlockSpecs_1.CodeBlockNodeAttr.Lang, newLang);
                                        contentDOM.classList.add(newLang);
                                    }
                                    else {
                                        dom.removeAttribute(CodeBlockSpecs_1.CodeBlockNodeAttr.Lang);
                                    }
                                    prevLang = newLang;
                                }
                                return true;
                            },
                        };
                    },
                },
            },
        });
    });
    function getDecorations(doc) {
        const decos = [];
        if (!lowlight) {
            return prosemirror_view_1.DecorationSet.empty;
        }
        for (const { node, pos } of (0, prosemirror_utils_1.findChildrenByType)(doc, (0, CodeBlockSpecs_1.codeBlockType)(doc.type.schema), true)) {
            let from = pos + 1;
            let nodes;
            const lang = node.attrs[CodeBlockSpecs_1.CodeBlockNodeAttr.Lang];
            if (lang && lowlight.registered(lang)) {
                nodes = lowlight.highlight(lang, node.textContent).children;
            }
            else {
                continue;
            }
            for (const { text, classes } of parseNodes(nodes)) {
                const to = from + text.length;
                if (classes.length) {
                    decos.push(prosemirror_view_1.Decoration.inline(from, to, {
                        class: classes.join(' '),
                    }));
                }
                from = to;
            }
        }
        return prosemirror_view_1.DecorationSet.create(doc, decos);
    }
};
exports.CodeBlockHighlight = CodeBlockHighlight;
function parseNodes(nodes, className = []) {
    return nodes
        .map((node) => {
        let classes = className;
        if (node.type === 'element') {
            classes = classes.concat(node.properties.className ?? []);
            return parseNodes(node.children, classes);
        }
        return {
            text: node.type === 'comment' || node.type === 'text' ? node.value : '',
            classes,
        };
    })
        .flat();
}
function stepHasFromTo(step) {
    // @ts-expect-error
    return typeof step.from === 'number' && typeof step.to === 'number';
}
//# sourceMappingURL=CodeBlockHighlight.js.map
