"use strict";
/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Cursor
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.CursorPopupMenu = void 0;
const React = require("react");
const core_react_1 = require("@itwin/core-react"); // ContextSubMenu,
const SessionState_1 = require("../../redux/SessionState");
const MenuItem_1 = require("../../shared/MenuItem");
const SyncUiEventDispatcher_1 = require("../../syncui/SyncUiEventDispatcher");
const UiFramework_1 = require("../../UiFramework");
const core_bentley_1 = require("@itwin/core-bentley");
/** Popup Menu to show at cursor typically used by tools to provide a right-click context menu.
 * @alpha
 */
// istanbul ignore next
class CursorPopupMenu extends React.PureComponent {
    constructor() {
        super(...arguments);
        this._isMounted = false; // used to ensure _handleSyncUiEvent callback is not processed after componentWillUnmount is called
        /** @internal */
        this.state = {
            menuX: 0,
            menuY: 0,
            menuVisible: false,
            items: undefined,
        };
        this._handleSyncUiEvent = (args) => {
            /* istanbul ignore next */
            if (!this._isMounted)
                return;
            /* istanbul ignore else */
            if (SyncUiEventDispatcher_1.SyncUiEventDispatcher.hasEventOfInterest(args.eventIds, [
                SessionState_1.SessionStateActionId.UpdateCursorMenu,
            ])) {
                const menuData = UiFramework_1.UiFramework.getCursorMenuData();
                if (menuData && this._hostChildWindowId === menuData.childWindowId) {
                    this.setState({
                        menuVisible: menuData.items && menuData.items.length > 0,
                        items: menuData.items,
                        menuX: menuData.position.x,
                        menuY: menuData.position.y,
                    });
                }
                else {
                    this.setState({ menuVisible: false, items: undefined });
                }
            }
        };
        this._handleRefSet = (popupDiv) => {
            var _a, _b;
            const parentWindow = (_a = popupDiv === null || popupDiv === void 0 ? void 0 : popupDiv.ownerDocument.defaultView) !== null && _a !== void 0 ? _a : undefined;
            // if the window is not a pop out set to undefined
            this._hostChildWindowId = UiFramework_1.UiFramework.childWindows.findId(parentWindow);
            core_bentley_1.Logger.logInfo(UiFramework_1.UiFramework.loggerCategory(UiFramework_1.UiFramework), `Cursor Menu for ${(_b = this._hostChildWindowId) !== null && _b !== void 0 ? _b : "main"} window`);
        };
        this._hideContextMenu = () => {
            this.setState({ menuVisible: false });
        };
        this._itemPicked = () => {
            this._hideContextMenu();
        };
    }
    componentDidMount() {
        this._isMounted = true;
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.onSyncUiEvent.addListener(this._handleSyncUiEvent);
    }
    componentWillUnmount() {
        this._isMounted = false;
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.onSyncUiEvent.removeListener(this._handleSyncUiEvent);
    }
    render() {
        const { menuX, menuY, items, menuVisible } = this.state;
        const onClose = this._hideContextMenu;
        return (React.createElement("div", { className: "uifw-cursor-menu-container-div", ref: this._handleRefSet }, items && items.length > 0 && menuVisible && (React.createElement(core_react_1.GlobalContextMenu, { className: this.props.className, style: this.props.style, identifier: "cursor-popup-menu", x: menuX, y: menuY, opened: menuVisible, onEsc: onClose, onOutsideClick: onClose, edgeLimit: false, autoflip: true }, MenuItem_1.MenuItemHelpers.createMenuItemNodes(MenuItem_1.MenuItemHelpers.createMenuItems(items, this._itemPicked))))));
    }
}
exports.CursorPopupMenu = CursorPopupMenu;
//# sourceMappingURL=CursorMenu.js.map