"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cutAutoOpenPlugin = void 0;
const prosemirror_state_1 = require("prosemirror-state");
// @ts-ignore // TODO: fix cjs build
const prosemirror_utils_1 = require("prosemirror-utils");
const lodash_1 = require("../../../../lodash.js");
const selection_1 = require("../../../../utils/selection.js");
const const_1 = require("../const.js");
const key = new prosemirror_state_1.PluginKey('yfm-cut-auto-open');
const cutAutoOpenPlugin = () => {
    return new prosemirror_state_1.Plugin({
        key,
        view(view) {
            update(view);
            const dragHandler = new CutAutoOpenOnDragOver(view);
            return {
                update: (view) => update(view),
                destroy: () => dragHandler.destroy(),
            };
        },
    });
};
exports.cutAutoOpenPlugin = cutAutoOpenPlugin;
function update(view) {
    const sel = view.state.selection;
    const domAtPos = view.domAtPos.bind(view);
    if ((0, selection_1.isTextSelection)(sel)) {
        if (sel.$cursor) {
            openParentYfmCuts(sel.$cursor, domAtPos);
        }
        else {
            openParentYfmCuts(sel.$head, domAtPos);
            openParentYfmCuts(sel.$anchor, domAtPos);
        }
    }
}
function openParentYfmCuts($pos, domAtPos) {
    let { depth } = $pos;
    const { schema } = $pos.parent.type;
    while (depth > 0) {
        if ($pos.node(depth).type === (0, const_1.cutContentType)(schema)) {
            if ($pos.node(depth - 1).type === (0, const_1.cutType)(schema)) {
                const { node: cutDomNode } = domAtPos($pos.start(depth - 1), 0);
                cutDomNode.classList.add('open');
                depth--;
            }
        }
        depth--;
    }
}
class CutAutoOpenOnDragOver {
    static YFM_CUT_SELECTOR = '.yfm-cut:not(.open)';
    static OPEN_TIMEOUT = 500; //ms
    static THROTTLE_WAIT = 50; //ms
    _cutElem = null;
    _editorView;
    _timeout = null;
    _docListener;
    constructor(view) {
        this._editorView = view;
        this._docListener = (0, lodash_1.throttle)(this._onDocEvent.bind(this), CutAutoOpenOnDragOver.THROTTLE_WAIT);
        document.addEventListener('mousemove', this._docListener);
        document.addEventListener('dragover', this._docListener);
    }
    destroy() {
        this._clear();
        this._docListener.cancel();
        document.removeEventListener('mousemove', this._docListener);
        document.removeEventListener('dragover', this._docListener);
    }
    _onDocEvent(event) {
        const view = this._editorView;
        if (!view.dragging)
            return;
        const pos = view.posAtCoords({ left: event.clientX, top: event.clientY });
        if (!pos)
            return;
        const elem = (0, prosemirror_utils_1.findDomRefAtPos)(pos.pos, view.domAtPos.bind(view));
        const cutElem = elem.closest(CutAutoOpenOnDragOver.YFM_CUT_SELECTOR);
        if (cutElem === this._cutElem)
            return;
        this._clear();
        if (cutElem)
            this._setCutElem(cutElem);
    }
    _clear() {
        if (this._timeout !== null)
            clearTimeout(this._timeout);
        this._timeout = null;
        this._cutElem = null;
    }
    _setCutElem(elem) {
        this._cutElem = elem;
        this._timeout = setTimeout(this._openCut.bind(this), CutAutoOpenOnDragOver.OPEN_TIMEOUT);
    }
    _openCut() {
        if (this._editorView.dragging) {
            this._cutElem?.classList.add('open');
        }
        this._clear();
    }
}
//# sourceMappingURL=auto-open.js.map
