"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.InternalKeyboardShortcutManager = void 0;
const appui_abstract_1 = require("@itwin/appui-abstract");
const CursorInformation_1 = require("../cursor/CursorInformation");
const KeyboardShortcutMenu_1 = require("./KeyboardShortcutMenu");
const KeyboardShortcut_1 = require("./KeyboardShortcut");
const SyncUiEventDispatcher_1 = require("../syncui/SyncUiEventDispatcher");
/** Keyboard Shortcut Manager
 * @internal
 */
class InternalKeyboardShortcutManager {
    /** Initialize the Keyboard Shortcut manager
     * @internal
     */
    static initialize() {
        SyncUiEventDispatcher_1.SyncUiEventDispatcher.onSyncUiEvent.addListener(InternalKeyboardShortcutManager._handleSyncUiEvent);
    }
    /** Loads Keyboard Shortcuts into the managed list */
    static loadShortcuts(shortcutList) {
        shortcutList.forEach((shortcutProps) => {
            this.loadShortcut(shortcutProps);
        });
    }
    /** Loads a Keyboard Shortcut into the managed list */
    static loadShortcut(shortcutProps) {
        const shortcut = new KeyboardShortcut_1.KeyboardShortcut(shortcutProps);
        this._shortcuts.registerKey(shortcut.keyMapKey, shortcut);
    }
    /** Processes a keystroke and invokes a matching Keyboard Shortcut */
    static processKey(keyboardKey, isAltKeyPressed = false, isCtrlKeyPressed = false, isShiftKeyPressed = false) {
        const keyMapKey = KeyboardShortcut_1.KeyboardShortcutContainer.generateKeyMapKey(keyboardKey, isAltKeyPressed, isCtrlKeyPressed, isShiftKeyPressed);
        const shortcut = this.getShortcut(keyMapKey);
        if (shortcut) {
            shortcut.itemPicked();
            return true;
        }
        return false;
    }
    /** Returns the managed list of Keyboard Shortcuts */
    static get shortcutContainer() {
        return this._shortcuts;
    }
    /** Returns a Keyboard Shortcut from the managed lists */
    static getShortcut(keyMapKey) {
        return this._shortcuts.findKey(keyMapKey);
    }
    /** Determines if focus is set to Home */
    static get isFocusOnHome() {
        const element = document.activeElement;
        return element && element === document.body;
    }
    /** Sets focus to Home */
    static setFocusToHome() {
        const element = document.activeElement;
        if (element && element !== document.body) {
            element.blur();
            document.body.focus();
        }
    }
    /** Displays the Keyboard Shortcuts menu at the cursor */
    static displayMenu() {
        if (this._shortcuts.areKeyboardShortcutsAvailable()) {
            this._shortcuts.showShortcutsMenu();
        }
    }
    /** Closes the Keyboard Shortcuts menu */
    static closeMenu() {
        KeyboardShortcutMenu_1.KeyboardShortcutMenu.onKeyboardShortcutMenuEvent.emit({
            menuVisible: false,
            menuX: 0,
            menuY: 0,
            shortcuts: undefined,
        });
    }
    /** Returns the cursor X position, which is mouseEvent.pageX. */
    static get cursorX() {
        return CursorInformation_1.CursorInformation.cursorX;
    }
    /** Returns the cursor Y position, which is mouseEvent.pageY. */
    static get cursorY() {
        return CursorInformation_1.CursorInformation.cursorY;
    }
}
InternalKeyboardShortcutManager._shortcuts = new KeyboardShortcut_1.KeyboardShortcutContainer();
InternalKeyboardShortcutManager._handleSyncUiEvent = (args) => {
    const updateBooleanValue = (booleanValue) => {
        if (SyncUiEventDispatcher_1.SyncUiEventDispatcher.hasEventOfInterest(args.eventIds, booleanValue.syncEventIds))
            booleanValue.refresh();
    };
    const handleForSyncIds = (shortcut) => {
        if (shortcut.isDisabled instanceof appui_abstract_1.ConditionalBooleanValue)
            updateBooleanValue(shortcut.isDisabled);
        if (shortcut.isHidden instanceof appui_abstract_1.ConditionalBooleanValue)
            updateBooleanValue(shortcut.isHidden);
    };
    InternalKeyboardShortcutManager._traverseShortcuts(InternalKeyboardShortcutManager._shortcuts.getAvailableKeyboardShortcuts(), handleForSyncIds);
};
InternalKeyboardShortcutManager._traverseShortcuts = (shortcuts, callback) => {
    shortcuts.forEach((shortcut) => {
        callback(shortcut);
        if (shortcut.shortcutContainer.areKeyboardShortcutsAvailable()) {
            const childShortcuts = shortcut.shortcutContainer.getAvailableKeyboardShortcuts();
            InternalKeyboardShortcutManager._traverseShortcuts(childShortcuts, callback);
        }
    });
};
exports.InternalKeyboardShortcutManager = InternalKeyboardShortcutManager;
//# sourceMappingURL=InternalKeyboardShortcut.js.map