"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TooltipView = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const logger_1 = require("../../../logger.js");
const ErrorBoundary_1 = require("../../../react-utils/ErrorBoundary.js");
const ReactRenderer_1 = require("../ReactRenderer/index.js");
const TextSelectionTooltip_1 = require("./TextSelectionTooltip.js");
class TooltipView {
    #isTooltipOpen = false;
    logger;
    actions;
    menuConfig;
    placement;
    onPopupOpenChange;
    view;
    visible = false;
    anchor = undefined;
    _tooltipRenderItem = null;
    constructor(actions, menuConfig, logger, params) {
        this.logger = logger;
        this.actions = actions;
        this.menuConfig = menuConfig;
        const { flip, placement = 'bottom', onPopupOpenChange } = params;
        this.placement = flip ? placement : [placement];
        this.onPopupOpenChange = onPopupOpenChange;
    }
    get isTooltipOpen() {
        return this.#isTooltipOpen;
    }
    show(view) {
        this.view = view;
        this.#isTooltipOpen = true;
        this.visible = true;
        this.anchor ??= this.createVirtualElement(view);
        this.renderPopup();
    }
    hide(view) {
        this.view = view;
        // do not rerender popup if it is already hidden
        if (!this.#isTooltipOpen && !this.visible)
            return;
        this.#isTooltipOpen = false;
        this.visible = false;
        this.anchor = undefined;
        this.renderPopup();
    }
    destroy() {
        this._tooltipRenderItem?.remove();
        this._tooltipRenderItem = null;
    }
    handleFocus = () => this.view.focus();
    handleClick = (id) => {
        logger_1.globalLogger.action({ mode: 'wysiwyg', source: 'context-menu', action: id });
        this.logger.action({ source: 'context-menu', action: id });
    };
    renderPopup() {
        this.tooltipRenderItem.rerender();
    }
    get tooltipRenderItem() {
        if (!this._tooltipRenderItem) {
            const reactRenderer = (0, ReactRenderer_1.getReactRendererFromState)(this.view.state);
            this._tooltipRenderItem = reactRenderer.createItem('selection_context', () => {
                if (!this.visible)
                    return null;
                return ((0, jsx_runtime_1.jsx)(ErrorBoundary_1.ErrorLoggerBoundary, { children: (0, jsx_runtime_1.jsx)(TextSelectionTooltip_1.TextSelectionTooltip, { config: this.menuConfig, editor: this.actions, editorView: this.view, focus: this.handleFocus, onClick: this.handleClick, popupPlacement: this.placement, popupAnchor: this.anchor, popupOnOpenChange: this.onPopupOpenChange }) }));
            });
        }
        return this._tooltipRenderItem;
    }
    createVirtualElement(view) {
        return {
            getBoundingClientRect() {
                // These are in screen coordinates
                const start = view.coordsAtPos(view.state.selection.from);
                const end = view.coordsAtPos(view.state.selection.to);
                // Find a center-ish x position from the selection endpoints (when
                // crossing lines, end may be more to the left)
                const yCenter = Math.max((start.left + end.left) / 2, start.left + 3);
                const top = start.top;
                const left = yCenter - 1;
                const width = 2;
                const height = end.bottom - start.top;
                return {
                    top,
                    left,
                    right: left + width,
                    bottom: top + height,
                    y: top,
                    x: left,
                    height,
                    width,
                };
            },
        };
    }
}
exports.TooltipView = TooltipView;
//# sourceMappingURL=tooltip.js.map
