"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.enableModeKeyboard = void 0;
const f_1 = require("../../../system/f/object/Keys/f");
const MODE_TO_KEY_1 = require("../../component/app/graph/MODE_TO_KEY");
const keyboard_1 = require("../../event/keyboard");
const KEY_TO_MODE_1 = require("../constant/KEY_TO_MODE");
const enableModeKeyboard = (component, callback) => {
    const { $system } = component;
    const _mode_keydown = {};
    const shortcuts = [];
    for (const mode in MODE_TO_KEY_1.MODE_TO_KEY) {
        const mode_key = MODE_TO_KEY_1.MODE_TO_KEY[mode];
        if ((0, keyboard_1.isKeyPressed)($system, mode_key)) {
            _mode_keydown[mode_key] = true;
            callback(mode);
        }
        shortcuts.push({
            combo: mode_key,
            strict: false,
            multiple: false,
            keydown: (key, event) => {
                const { ctrlKey } = event;
                if (ctrlKey) {
                    return;
                }
                if (mode_key === key) {
                    _mode_keydown[key] = true;
                    callback(mode);
                }
            },
            keyup: (key) => {
                delete _mode_keydown[key];
                if (key === MODE_TO_KEY_1.MODE_TO_KEY[mode]) {
                    const mode_keydown = (0, f_1.keys)(_mode_keydown);
                    const mode_keydown_count = mode_keydown.length;
                    if (mode_keydown_count > 0) {
                        callback(KEY_TO_MODE_1.KEY_TO_MODE[mode_keydown[mode_keydown_count - 1]]);
                    }
                    else {
                        callback('none');
                    }
                }
            },
        });
    }
    const shortcutListener = (0, keyboard_1.makeShortcutListener)(shortcuts);
    const keyboard_unlisten = component.addEventListener(shortcutListener);
    return keyboard_unlisten;
};
exports.enableModeKeyboard = enableModeKeyboard;
//# sourceMappingURL=modes.js.map