/*---------------------------------------------------------------------------------------------
 * 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
 */
import * as React from "react";
import { GlobalContextMenu } from "@itwin/core-react"; // ContextSubMenu,
import { SessionStateActionId } from "../../redux/SessionState";
import { MenuItemHelpers } from "../../shared/MenuItem";
import { SyncUiEventDispatcher } from "../../syncui/SyncUiEventDispatcher";
import { UiFramework } from "../../UiFramework";
import { Logger } from "@itwin/core-bentley";
/** Popup Menu to show at cursor typically used by tools to provide a right-click context menu.
 * @alpha
 */
// istanbul ignore next
export 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.hasEventOfInterest(args.eventIds, [
                SessionStateActionId.UpdateCursorMenu,
            ])) {
                const menuData = 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) => {
            const parentWindow = popupDiv?.ownerDocument.defaultView ?? undefined;
            // if the window is not a pop out set to undefined
            this._hostChildWindowId = UiFramework.childWindows.findId(parentWindow);
            Logger.logInfo(UiFramework.loggerCategory(UiFramework), `Cursor Menu for ${this._hostChildWindowId ?? "main"} window`);
        };
        this._hideContextMenu = () => {
            this.setState({ menuVisible: false });
        };
        this._itemPicked = () => {
            this._hideContextMenu();
        };
    }
    componentDidMount() {
        this._isMounted = true;
        SyncUiEventDispatcher.onSyncUiEvent.addListener(this._handleSyncUiEvent);
    }
    componentWillUnmount() {
        this._isMounted = false;
        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(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 }, MenuItemHelpers.createMenuItemNodes(MenuItemHelpers.createMenuItems(items, this._itemPicked))))));
    }
}
//# sourceMappingURL=CursorMenu.js.map