"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.KeyboardShortcutContainer = exports.KeyboardShortcut = void 0;
const React = require("react");
const appui_abstract_1 = require("@itwin/appui-abstract");
const ItemDefBase_1 = require("../shared/ItemDefBase");
const UiFramework_1 = require("../UiFramework");
const KeyboardShortcutMenu_1 = require("./KeyboardShortcutMenu");
/* eslint-disable deprecation/deprecation */
var FunctionKey;
(function (FunctionKey) {
    FunctionKey["F1"] = "F1";
    FunctionKey["F2"] = "F2";
    FunctionKey["F3"] = "F3";
    FunctionKey["F4"] = "F4";
    FunctionKey["F5"] = "F5";
    FunctionKey["F6"] = "F6";
    FunctionKey["F7"] = "F7";
    FunctionKey["F8"] = "F8";
    FunctionKey["F9"] = "F9";
    FunctionKey["F10"] = "F10";
    FunctionKey["F11"] = "F11";
    FunctionKey["F12"] = "F12";
})(FunctionKey || (FunctionKey = {}));
var SpecialKey;
(function (SpecialKey) {
    SpecialKey["Home"] = "Home";
    SpecialKey["End"] = "End";
    SpecialKey["PageUp"] = "PageUp";
    SpecialKey["PageDown"] = "PageDown";
    SpecialKey["Escape"] = "Escape";
    SpecialKey["Delete"] = "Delete";
    SpecialKey["Insert"] = "Insert";
    SpecialKey["Tab"] = "Tab";
    SpecialKey["ArrowLeft"] = "ArrowLeft";
    SpecialKey["ArrowRight"] = "ArrowRight";
    SpecialKey["ArrowUp"] = "ArrowUp";
    SpecialKey["ArrowDown"] = "ArrowDown";
    SpecialKey["Enter"] = "Enter";
    SpecialKey["Return"] = "Enter";
    SpecialKey["Space"] = " ";
    SpecialKey["Backspace"] = "Backspace";
    SpecialKey["Clear"] = "Clear";
    SpecialKey["Divide"] = "Divide";
    SpecialKey["Multiply"] = "Multiply";
    SpecialKey["Subtract"] = "Subtract";
    SpecialKey["Add"] = "Add";
    SpecialKey["Decimal"] = "Decimal";
})(SpecialKey || (SpecialKey = {}));
/** Keyboard Shortcut used to execute an action
 * @public
 * @deprecated in 4.15.0. Use {@link KeyboardShortcutProps} or {@link FrameworkKeyboardShortcut} instead.
 */
class KeyboardShortcut extends ItemDefBase_1.ItemDefBase {
    constructor(props) {
        super(props);
        this._isAltKeyRequired = false;
        this._isCtrlKeyRequired = false;
        this._isShiftKeyRequired = false;
        this._isFunctionKey = false;
        this._isSpecialKey = false;
        this._key = props.key;
        if (this._key in FunctionKey)
            this._isFunctionKey = true;
        if (this._key in SpecialKey)
            this._isSpecialKey = true;
        this._shortcuts = new KeyboardShortcutContainer();
        if (props.item) {
            this._item = props.item;
            // Copy over icon, label & tooltip from the item
            if (!this.iconSpec)
                this.iconSpec = this._item.iconSpec;
            if (!this.label)
                this.setLabel(this._item.label);
            if (!this.tooltip)
                this.setTooltip(this._item.tooltip);
            if (this.isDisabled === undefined)
                this.isDisabled = this._item.isDisabled;
            if (this.isHidden === undefined)
                this.isHidden = this._item.isHidden;
        }
        else if (props.execute) {
            this._execute = props.execute;
        }
        else if (props.shortcuts) {
            props.shortcuts.forEach((childProps) => {
                const shortcut = new KeyboardShortcut(childProps);
                this._shortcuts.registerKey(shortcut.keyMapKey, shortcut);
            });
        }
        else {
            throw new appui_abstract_1.UiError(UiFramework_1.UiFramework.loggerCategory(this), `Either 'item', 'execute' or 'shortcuts' must be specified for '${props.key}' key.`);
        }
        if (props.isAltKeyRequired !== undefined)
            this._isAltKeyRequired = props.isAltKeyRequired;
        if (props.isCtrlKeyRequired !== undefined)
            this._isCtrlKeyRequired = props.isCtrlKeyRequired;
        if (props.isShiftKeyRequired !== undefined)
            this._isShiftKeyRequired = props.isShiftKeyRequired;
        if (props.iconNode !== undefined)
            this.iconSpec = React.createElement(React.Fragment, null, props.iconNode);
    }
    /** Returns the id for this shortcut */
    get id() {
        return this.keyMapKey;
    }
    /** Returns the shortcut container */
    get shortcutContainer() {
        return this._shortcuts;
    }
    /** Finds a shortcut with a given key in the shortcut's container */
    getShortcut(mapKey) {
        return this._shortcuts.findKey(mapKey);
    }
    /** Returns the shortcut's key map key used as the id */
    get keyMapKey() {
        const keyMapKey = KeyboardShortcutContainer.generateKeyMapKey(this.key, this._isAltKeyRequired, this._isCtrlKeyRequired, this._isShiftKeyRequired);
        return keyMapKey;
    }
    /** Returns the [[ActionButtonItemDef]] associated with this shortcut */
    get item() {
        return this._item;
    }
    /** Called when the [[ActionButtonItemDef]] associated with this shortcut is invoked */
    itemPicked() {
        if (this._shortcuts.areKeyboardShortcutsAvailable()) {
            this._shortcuts.showShortcutsMenu();
        }
        else {
            setTimeout(() => {
                if (this._item)
                    this._item.execute();
                else if (this._execute)
                    this._execute();
            });
        }
    }
    /** Gets the keyboard key */
    get key() {
        return this._key;
    }
    /** Gets whether the Alt key required. */
    get isAltKeyRequired() {
        return this._isAltKeyRequired;
    }
    /** Gets whether the Ctrl key required. */
    get isCtrlKeyRequired() {
        return this._isCtrlKeyRequired;
    }
    /** Gets whether the Shift key required. */
    get isShiftKeyRequired() {
        return this._isShiftKeyRequired;
    }
    /** Gets whether this is a Function key. */
    get isFunctionKey() {
        return this._isFunctionKey;
    }
    /** Gets whether this is a Special key. */
    get isSpecialKey() {
        return this._isSpecialKey;
    }
}
exports.KeyboardShortcut = KeyboardShortcut;
/** Keyboard Shortcut Container
 * @public
 * @deprecated in 4.15.0. Use {@link FrameworkKeyboardShortcutContainer} instead.
 */
class KeyboardShortcutContainer {
    constructor() {
        this._keyMap = new Map();
        this._keyArray = new Array();
    }
    /** Registers a Keyboard Shortcut associated with a given key in the managed list */
    registerKey(keyMapKey, inShortcut) {
        let shortcut;
        if ((shortcut = this.findKey(keyMapKey)) === undefined) {
            shortcut = inShortcut;
            this._keyArray.push(shortcut);
        }
        else {
            const index = this._keyArray.findIndex((value) => {
                return value.keyMapKey === keyMapKey;
            });
            if (index >= 0) {
                shortcut = inShortcut;
                this._keyArray[index] = shortcut;
            }
        }
        if (shortcut)
            this._keyMap.set(keyMapKey, shortcut);
        return shortcut;
    }
    /** Finds a Keyboard Shortcut associated with a given key */
    findKey(keyMapKey) {
        return this._keyMap.get(keyMapKey);
    }
    /** Determines if any Keyboard Shortcuts are available in this container */
    areKeyboardShortcutsAvailable() {
        return this._keyMap.size !== 0;
    }
    /** Empties any Keyboard Shortcuts from this container */
    emptyData() {
        this._keyMap.clear();
        this._keyArray.length = 0;
    }
    getAvailableKeyboardShortcuts() {
        return this._keyArray.slice();
    }
    /** Generates a key used for storing and finding the Keyboard Shortcuts in this container */
    static generateKeyMapKey(keyboardKey, isAltKeyRequired, isCtrlKeyRequired, isShiftKeyRequired) {
        let keyMapKey = keyboardKey;
        if (isAltKeyRequired)
            keyMapKey = `Alt+${keyMapKey}`;
        if (isShiftKeyRequired)
            keyMapKey = `Shift+${keyMapKey}`;
        if (isCtrlKeyRequired)
            keyMapKey = `Ctrl+${keyMapKey}`;
        return keyMapKey;
    }
    /** Displays a menu for the Keyboard Shortcuts in this container */
    showShortcutsMenu() {
        const offset = 8;
        KeyboardShortcutMenu_1.KeyboardShortcutMenu.onKeyboardShortcutMenuEvent.emit({
            menuVisible: true,
            menuX: UiFramework_1.UiFramework.keyboardShortcuts.cursorX - offset,
            menuY: UiFramework_1.UiFramework.keyboardShortcuts.cursorY - offset,
            shortcuts: this.getAvailableKeyboardShortcuts(),
        });
    }
}
exports.KeyboardShortcutContainer = KeyboardShortcutContainer;
//# sourceMappingURL=KeyboardShortcut.js.map