"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "createKeyboardShortcutsCtrl", {
    enumerable: true,
    get: function() {
        return createKeyboardShortcutsCtrl;
    }
});
const _defaultShortcutsList = require("./defaultShortcutsList");
const SHORTCUTS_CONTEXT = 'menu';
const SHORTCUTS_GROUP = SHORTCUTS_CONTEXT;
function createKeyboardShortcutsCtrl(menu, customKeyboardShortcuts = []) {
    const customShortcuts = [];
    _addShortcuts((0, _defaultShortcutsList.createDefaultShortcutsList)(menu));
    customKeyboardShortcuts.forEach(({ shortcuts, contextName })=>{
        addCustomShortcuts(shortcuts, contextName);
    });
    /**
   * Adds keyboard shortcuts to the menu.
   *
   * @param {KeyboardShortcut[]} shortcuts Keyboard shortcuts to add.
   * @param {string} [contextName] The context name to create or use.
   */ function _addShortcuts(shortcuts, contextName) {
        getContext(contextName).addShortcuts(shortcuts, {
            group: SHORTCUTS_CONTEXT
        });
    }
    /**
   * Adds custom keyboard shortcuts to the menu.
   *
   * @param {KeyboardShortcut[]} shortcuts Keyboard shortcuts to add.
   * @param {string} [contextName] The context name to create or use.
   */ function addCustomShortcuts(shortcuts, contextName) {
        const context = getContext(contextName);
        shortcuts.forEach(({ keys })=>{
            keys.forEach((k)=>context.removeShortcutsByKeys(k));
        });
        customShortcuts.push({
            shortcuts,
            contextName
        });
        _addShortcuts(shortcuts, contextName);
    }
    /**
   * Gets all registered custom keyboard shortcuts.
   *
   * @returns {Array<{ shortcuts: KeyboardShortcut, contextName: string }>}
   */ function getCustomShortcuts() {
        return [
            ...customShortcuts
        ];
    }
    /**
   * Gets the context name.
   *
   * @param {string} contextName The context name.
   * @returns {string}
   */ function _getContextName(contextName) {
        return contextName ? `${SHORTCUTS_GROUP}:${contextName}` : SHORTCUTS_GROUP;
    }
    /**
   * Gets the keyboard shortcut context by its name.
   *
   * @param {string} contextName The context name.
   * @returns {object}
   */ function getContext(contextName) {
        const manager = menu.hotMenu.getShortcutManager();
        const name = _getContextName(contextName);
        return manager.getContext(name) ?? manager.addContext(name);
    }
    /**
   * Makes the specified context active.
   *
   * @param {string} [contextName] The context name.
   */ function listen(contextName) {
        menu.hotMenu.getShortcutManager().setActiveContextName(_getContextName(contextName));
    }
    return {
        addCustomShortcuts,
        getCustomShortcuts,
        getContext,
        listen
    };
}
