/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module KeyboardShortcut
 */
import * as React from "react";
import { ContextMenuItem, ContextSubMenu, GlobalContextMenu, } from "@itwin/core-react";
import { ConditionalBooleanValue, UiEvent } from "@itwin/appui-abstract";
/** KeyboardShortcut Menu Event class.
 * @public
 * @deprecated in 4.13.0. This class should not be used by applications to instantiate objects.
 */
export class KeyboardShortcutMenuEvent extends UiEvent {
}
/** React component that displays a context menu at the cursor containing keyboard shortcuts.
 * @public
 * @deprecated in 4.15.0. Component used internally to display keyboard shortcuts in a context menu.
 */
export class KeyboardShortcutMenu extends React.PureComponent {
    state = {
        menuVisible: false,
        menuX: 0,
        menuY: 0,
    };
    /** Get KeyboardShortcut Menu Event. */
    static onKeyboardShortcutMenuEvent = new KeyboardShortcutMenuEvent();
    componentDidMount() {
        KeyboardShortcutMenu.onKeyboardShortcutMenuEvent.addListener(this._handleKeyboardShortcutMenuEvent);
    }
    componentWillUnmount() {
        KeyboardShortcutMenu.onKeyboardShortcutMenuEvent.removeListener(this._handleKeyboardShortcutMenuEvent);
    }
    _handleKeyboardShortcutMenuEvent = (state) => {
        this.setState(state);
    };
    render() {
        const { shortcuts, menuX, menuY, menuVisible } = this.state;
        const onClose = this._hideContextMenu;
        if (menuVisible) {
            const items = this.getShortcutMenuItems(shortcuts);
            return (React.createElement(GlobalContextMenu, { className: this.props.className, style: this.props.style, identifier: "keyboard-shortcuts", x: menuX, y: menuY, opened: menuVisible, onEsc: onClose, onOutsideClick: onClose, edgeLimit: false, autoflip: true, ignoreNextKeyUp: true, offset: 12 }, items));
        }
        return null;
    }
    getShortcutMenuItems(shortcuts) {
        const items = [];
        if (shortcuts) {
            shortcuts.forEach((shortcut, index) => {
                const item = this.getShortcutMenuItem(shortcut, index);
                if (item)
                    items.push(item);
            });
        }
        return items;
    }
    getShortcutMenuItem(shortcut, index) {
        const shortcutKey = shortcut.key;
        const isHidden = ConditionalBooleanValue.getValue(shortcut.isHidden);
        // Only pure characters go into the context menu
        if (shortcutKey !== shortcut.keyMapKey ||
            shortcut.isFunctionKey ||
            shortcut.isSpecialKey ||
            isHidden)
            return null;
        let node = null;
        let label = shortcut.label;
        const iconSpec = shortcut.iconSpec;
        label = `~${shortcutKey.toLocaleUpperCase()} ${label}`;
        if (shortcut.shortcutContainer.areKeyboardShortcutsAvailable()) {
            const shortcuts = shortcut.shortcutContainer.getAvailableKeyboardShortcuts();
            const items = this.getShortcutMenuItems(shortcuts);
            node = (React.createElement(ContextSubMenu, { key: index, icon: iconSpec, label: label, disabled: shortcut.isDisabled, badgeType: shortcut.badgeType, badgeKind: shortcut.badgeKind }, items));
        }
        else {
            const sel = () => this._itemPicked(shortcut);
            node = (React.createElement(ContextMenuItem, { key: index, onSelect: sel, icon: iconSpec, disabled: shortcut.isDisabled, badgeType: shortcut.badgeType, badgeKind: shortcut.badgeKind }, label));
        }
        return node;
    }
    _hideContextMenu = () => {
        this.setState({ menuVisible: false, shortcuts: undefined });
    };
    _itemPicked = (shortcut) => {
        this._hideContextMenu();
        shortcut.itemPicked();
    };
}
//# sourceMappingURL=KeyboardShortcutMenu.js.map