"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cutActivePlugin = void 0;
const state_1 = require("../../../../pm/state.js");
const view_1 = require("../../../../pm/view.js");
const selection_1 = require("../../../../utils/selection.js");
const const_1 = require("../const.js");
const cutActivePlugin = () => {
    return new state_1.Plugin({
        props: {
            decorations(state) {
                const decos = [];
                const sel = state.selection;
                const yfmCutType = (0, const_1.cutType)(state.schema);
                const createDeco = ({ pos, node }) => {
                    decos.push(view_1.Decoration.node(pos, pos + node.nodeSize, {
                        class: const_1.YfmCutClassName.Active,
                    }));
                };
                if ((0, selection_1.isNodeSelection)(sel)) {
                    if (sel.node.type === yfmCutType)
                        createDeco({ pos: sel.from, node: sel.node });
                    findParentNodesOfType(yfmCutType, sel.$from).forEach(createDeco);
                }
                else if ((0, selection_1.isTextSelection)(sel)) {
                    findParentNodesOfType(yfmCutType, sel.$from).forEach(createDeco);
                    if (!sel.$from.sameParent(sel.$to)) {
                        findParentNodesOfType(yfmCutType, sel.$to).forEach(createDeco);
                        state.doc.nodesBetween(sel.from, sel.to, (node, pos) => {
                            if (node.type === yfmCutType)
                                createDeco({ pos, node });
                            if (node.isTextblock)
                                return false;
                            return true;
                        });
                    }
                }
                else {
                    // some other selection
                    findParentNodesOfType(yfmCutType, sel.$from).forEach(createDeco);
                }
                return decos.length ? view_1.DecorationSet.create(state.doc, decos) : view_1.DecorationSet.empty;
            },
        },
    });
};
exports.cutActivePlugin = cutActivePlugin;
function findParentNodesOfType(type, $pos) {
    let { depth } = $pos;
    const nodes = [];
    while (depth >= 0) {
        const node = $pos.node(depth);
        if (node.type === type)
            nodes.push({ depth, node, pos: $pos.before(depth) });
        depth--;
    }
    return nodes;
}
//# sourceMappingURL=active.js.map
