"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.switchTabById = exports.switchTabByElem = exports.atEndOfPanel = exports.execAfterPaint = void 0;
const const_1 = require("./const.js");
const execAfterPaint = (fn) => {
    requestAnimationFrame(() => {
        requestAnimationFrame(fn);
    });
};
exports.execAfterPaint = execAfterPaint;
const atEndOfPanel = (view) => {
    if (!view)
        return null;
    const { $head } = view.state.selection;
    for (let d = $head.depth; d >= 0; d--) {
        const parent = $head.node(d), index = $head.indexAfter(d);
        if (index !== parent.childCount)
            return false;
        if (parent.type === (0, const_1.tabPanelType)(view.state.schema)) {
            const panelPos = $head.before(d);
            return view?.endOfTextblock('down') ? panelPos : null;
        }
    }
    return null;
};
exports.atEndOfPanel = atEndOfPanel;
const switchTabByElem = (tabElem) => {
    if (tabElem.classList.contains(const_1.YFM_TAB_CLASSNAME)) {
        tabElem.click();
    }
};
exports.switchTabByElem = switchTabByElem;
const switchTabById = (container, tabId) => {
    const selector = `.${const_1.YFM_TAB_CLASSNAME}[${const_1.DIPLODOC_ID_ATTR}="${tabId}"]`;
    const tabElem = container.querySelector(selector);
    if (tabElem) {
        (0, exports.switchTabByElem)(tabElem);
    }
};
exports.switchTabById = switchTabById;
//# sourceMappingURL=utils.js.map
