/*---------------------------------------------------------------------------------------------
 * 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.js";
import { MenuItemHelpers } from "../../shared/MenuItem.js";
import { SyncUiEventDispatcher } from "../../syncui/SyncUiEventDispatcher.js";
import { UiFramework } from "../../UiFramework.js";
/** Popup Menu to show at cursor typically used by tools to provide a right-click context menu.
 * @alpha
 */
// eslint-disable-next-line @typescript-eslint/no-deprecated
export function CursorPopupMenu(props) {
    const windowIdRef = React.useRef(undefined);
    const [opened, setOpened] = React.useState(false);
    const [x, setX] = React.useState(0);
    const [y, setY] = React.useState(0);
    const [items, setItems] = React.useState(undefined);
    React.useEffect(() => {
        return SyncUiEventDispatcher.onSyncUiEvent.addListener((args) => {
            if (!SyncUiEventDispatcher.hasEventOfInterest(args.eventIds, [
                // eslint-disable-next-line @typescript-eslint/no-deprecated
                SessionStateActionId.UpdateCursorMenu,
            ]))
                return;
            const menuData = UiFramework.getCursorMenuData();
            const windowId = windowIdRef.current;
            if (!menuData || windowId !== menuData.childWindowId) {
                setOpened(false);
                setItems(undefined);
                return;
            }
            setOpened(menuData.items && menuData.items.length > 0);
            setItems(menuData.items);
            setX(menuData.position.x);
            setY(menuData.position.y);
        });
    }, []);
    const onClose = () => {
        setOpened(false);
    };
    return (React.createElement("div", { className: "uifw-cursor-menu-container-div", ref: (el) => {
            const parentWindow = el?.ownerDocument.defaultView ?? undefined;
            const windowId = UiFramework.childWindows.findId(parentWindow);
            windowIdRef.current = windowId;
        } }, items && items.length > 0 && opened && (
    // eslint-disable-next-line @typescript-eslint/no-deprecated
    React.createElement(GlobalContextMenu, { className: props.className, style: props.style, identifier: "cursor-popup-menu", x: x, y: y, opened: opened, onEsc: onClose, onOutsideClick: onClose, edgeLimit: false, autoflip: true }, MenuItemHelpers.createMenuItemNodes(
    // eslint-disable-next-line @typescript-eslint/no-deprecated
    MenuItemHelpers.createMenuItems(items, onClose))))));
}
//# sourceMappingURL=CursorMenu.js.map