"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 KeyboardShortcut
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.KeyboardShortcutMenu = exports.KeyboardShortcutMenuEvent = void 0;
const React = require("react");
const core_react_1 = require("@itwin/core-react");
const appui_abstract_1 = require("@itwin/appui-abstract");
/** KeyboardShortcut Menu Event class.
 * @public
 * @deprecated in 4.13.0. This class should not be used by applications to instantiate objects.
 */
class KeyboardShortcutMenuEvent extends appui_abstract_1.UiEvent {
}
exports.KeyboardShortcutMenuEvent = KeyboardShortcutMenuEvent;
/** 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.
 */
class KeyboardShortcutMenu extends React.PureComponent {
    constructor() {
        super(...arguments);
        this.state = {
            menuVisible: false,
            menuX: 0,
            menuY: 0,
        };
        this._handleKeyboardShortcutMenuEvent = (state) => {
            this.setState(state);
        };
        this._hideContextMenu = () => {
            this.setState({ menuVisible: false, shortcuts: undefined });
        };
        this._itemPicked = (shortcut) => {
            this._hideContextMenu();
            shortcut.itemPicked();
        };
    }
    componentDidMount() {
        KeyboardShortcutMenu.onKeyboardShortcutMenuEvent.addListener(this._handleKeyboardShortcutMenuEvent);
    }
    componentWillUnmount() {
        KeyboardShortcutMenu.onKeyboardShortcutMenuEvent.removeListener(this._handleKeyboardShortcutMenuEvent);
    }
    render() {
        const { shortcuts, menuX, menuY, menuVisible } = this.state;
        const onClose = this._hideContextMenu;
        if (menuVisible) {
            const items = this.getShortcutMenuItems(shortcuts);
            return (React.createElement(core_react_1.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 }, 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 = appui_abstract_1.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(core_react_1.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(core_react_1.ContextMenuItem, { key: index, onSelect: sel, icon: iconSpec, disabled: shortcut.isDisabled, badgeType: shortcut.badgeType, badgeKind: shortcut.badgeKind }, label));
        }
        return node;
    }
}
exports.KeyboardShortcutMenu = KeyboardShortcutMenu;
/** Get KeyboardShortcut Menu Event. */
KeyboardShortcutMenu.onKeyboardShortcutMenuEvent = new KeyboardShortcutMenuEvent();
//# sourceMappingURL=KeyboardShortcutMenu.js.map