import { findParentNodeClosestToPos } from "../../../../../../pm/utils.js";
import { isTableCellNode, isTableNode } from "../../../../../../table-utils/index.js";
export class DropCursor {
    editorView;
    width;
    color;
    class;
    cursorElem = null;
    cursorPos = null;
    constructor(editorView, params = {}) {
        this.editorView = editorView;
        this.width = params.width ?? 1;
        this.color = params.color === false ? undefined : params.color || 'black';
        this.class = params.class;
    }
    getPos() {
        return this.cursorPos;
    }
    setPos(val) {
        if (typeof val !== 'number' || isNaN(val))
            throw new Error('"val" is not a number');
        if (this.cursorPos === val)
            return;
        this.cursorPos = val;
        this.update();
    }
    clear() {
        this.cursorPos = null;
        this.cursorElem?.remove();
        this.cursorElem = null;
    }
    update() {
        const { cursorPos } = this;
        if (cursorPos === null)
            return;
        const $pos = this.editorView.state.doc.resolve(cursorPos);
        const isBlock = !$pos.parent.inlineContent;
        let rect;
        if (isBlock) {
            const before = $pos.nodeBefore, after = $pos.nodeAfter;
            if (before || after) {
                const node = this.editorView.nodeDOM(cursorPos - (before ? before.nodeSize : 0));
                if (node) {
                    const nodeRect = node.getBoundingClientRect();
                    let top = before ? nodeRect.bottom : nodeRect.top;
                    if (before && after)
                        top =
                            (top +
                                this.editorView.nodeDOM(cursorPos).getBoundingClientRect().top) /
                                2;
                    rect = {
                        left: nodeRect.left,
                        right: nodeRect.right,
                        top: top - this.width / 2,
                        bottom: top + this.width / 2,
                    };
                }
            }
        }
        if (!rect) {
            const coords = this.editorView.coordsAtPos(cursorPos);
            rect = {
                left: coords.left - this.width / 2,
                right: coords.left + this.width / 2,
                top: coords.top,
                bottom: coords.bottom,
            };
        }
        this.render(rect, { isBlock });
    }
    render(rect, { isBlock }) {
        const parent = this.editorView.dom.offsetParent;
        if (!this.cursorElem) {
            this.cursorElem = parent.appendChild(document.createElement('div'));
            if (this.class)
                this.cursorElem.className = this.class;
            this.cursorElem.style.cssText =
                'position: absolute; z-index: 50; pointer-events: none;';
            if (this.color) {
                this.cursorElem.style.backgroundColor = this.color;
            }
        }
        this.cursorElem.classList.toggle('prosemirror-dropcursor-block', isBlock);
        this.cursorElem.classList.toggle('prosemirror-dropcursor-inline', !isBlock);
        let parentLeft, parentTop;
        if (!parent ||
            (parent === document.body && getComputedStyle(parent).position === 'static')) {
            parentLeft = -pageXOffset;
            parentTop = -pageYOffset;
        }
        else {
            const rect = parent.getBoundingClientRect();
            parentLeft = rect.left - parent.scrollLeft;
            parentTop = rect.top - parent.scrollTop;
        }
        this.cursorElem.style.left = rect.left - parentLeft + 'px';
        this.cursorElem.style.top = rect.top - parentTop + 'px';
        this.cursorElem.style.width = rect.right - rect.left + 'px';
        this.cursorElem.style.height = rect.bottom - rect.top + 'px';
    }
}
export class TableColumnDropCursor extends DropCursor {
    update() {
        const cursorPos = this.getPos();
        if (cursorPos === null)
            return;
        const $cursorPos = this.editorView.state.doc.resolve(cursorPos);
        const parentTable = findParentNodeClosestToPos($cursorPos, isTableNode);
        if (!parentTable)
            return;
        let side;
        let tcellPos;
        if ($cursorPos.nodeAfter && isTableCellNode($cursorPos.nodeAfter)) {
            side = 'left';
            tcellPos = cursorPos;
        }
        else if ($cursorPos.nodeBefore && isTableCellNode($cursorPos.nodeBefore)) {
            side = 'right';
            tcellPos = cursorPos - $cursorPos.nodeBefore.nodeSize;
        }
        else {
            this.cursorElem?.remove();
            this.cursorElem = null;
            return;
        }
        const tdElem = this.editorView.nodeDOM(tcellPos);
        const tableElem = this.editorView.nodeDOM(parentTable.pos);
        const tdRect = tdElem.getBoundingClientRect();
        const tableRect = tableElem.getBoundingClientRect();
        const rect = {
            left: tdRect[side] - this.width / 2,
            right: tdRect[side] + this.width / 2,
            top: tableRect.top,
            bottom: tableRect.bottom,
        };
        this.render(rect, { isBlock: true });
    }
}
//# sourceMappingURL=dnd-drop-cursor.js.map
