"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get MODIFIER_KEYS () {
        return MODIFIER_KEYS;
    },
    get getEventKeyCombinations () {
        return getEventKeyCombinations;
    },
    get getKeysList () {
        return getKeysList;
    },
    get getPressedModifierKeys () {
        return getPressedModifierKeys;
    },
    get isModifierKey () {
        return isModifierKey;
    },
    get normalizeEventKey () {
        return normalizeEventKey;
    },
    get normalizeKeys () {
        return normalizeKeys;
    }
});
// This file handles key-name discrepancies between browsers.
// For the list of discrepancies, go to: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values.
const mappings = new Map([
    [
        ' ',
        'space'
    ],
    [
        'spacebar',
        'space'
    ],
    [
        'scroll',
        'scrolllock'
    ],
    [
        'del',
        'delete'
    ],
    [
        'esc',
        'escape'
    ],
    [
        'medianexttrack',
        'mediatracknext'
    ],
    [
        'mediaprevioustrack',
        'mediatrackprevious'
    ],
    [
        'volumeup',
        'audiovolumeup'
    ],
    [
        'volumedown',
        'audiovolumedown'
    ],
    [
        'volumemute',
        'audiovolumemute'
    ],
    [
        'multiply',
        '*'
    ],
    [
        'add',
        '+'
    ],
    [
        'divide',
        '/'
    ],
    [
        'subtract',
        '-'
    ],
    [
        'left',
        'arrowleft'
    ],
    [
        'right',
        'arrowright'
    ],
    [
        'up',
        'arrowup'
    ],
    [
        'down',
        'arrowdown'
    ]
]);
const normalizeKeys = (keys)=>{
    return keys.map((key)=>{
        const lowercaseKey = key.toLowerCase();
        if (mappings.has(lowercaseKey)) {
            return mappings.get(lowercaseKey);
        }
        return lowercaseKey;
    }).sort().join('+');
};
const getKeysList = (normalizedKeys)=>{
    return normalizedKeys.split('+');
};
const specialCharactersSet = new Map([
    [
        96,
        'numpad0'
    ],
    [
        97,
        'numpad1'
    ],
    [
        98,
        'numpad2'
    ],
    [
        99,
        'numpad3'
    ],
    [
        100,
        'numpad4'
    ],
    [
        101,
        'numpad5'
    ],
    [
        102,
        'numpad6'
    ],
    [
        103,
        'numpad7'
    ],
    [
        104,
        'numpad8'
    ],
    [
        105,
        'numpad9'
    ],
    [
        106,
        'multiply'
    ],
    [
        107,
        'add'
    ],
    [
        108,
        'decimal'
    ],
    [
        109,
        'subtract'
    ],
    [
        110,
        'decimal'
    ],
    [
        111,
        'divide'
    ],
    [
        112,
        'f1'
    ],
    [
        113,
        'f2'
    ],
    [
        114,
        'f3'
    ],
    [
        115,
        'f4'
    ],
    [
        116,
        'f5'
    ],
    [
        117,
        'f6'
    ],
    [
        118,
        'f7'
    ],
    [
        119,
        'f8'
    ],
    [
        120,
        'f9'
    ],
    [
        121,
        'f10'
    ],
    [
        122,
        'f11'
    ],
    [
        123,
        'f12'
    ],
    [
        186,
        'semicolon'
    ],
    [
        187,
        'equal'
    ],
    [
        188,
        'comma'
    ],
    [
        189,
        'minus'
    ],
    [
        190,
        'period'
    ],
    [
        191,
        'slash'
    ],
    [
        192,
        'backquote'
    ],
    [
        219,
        'bracketleft'
    ],
    [
        220,
        'backslash'
    ],
    [
        221,
        'bracketright'
    ],
    [
        222,
        'quote'
    ]
]);
const normalizeEventKey = ({ which, key })=>{
    if (specialCharactersSet.has(which)) {
        return specialCharactersSet.get(which);
    }
    const normalizedKey = String.fromCharCode(which).toLowerCase();
    if (/^[a-z0-9]$/.test(normalizedKey)) {
        return normalizedKey;
    }
    return key.toLowerCase();
};
const MODIFIER_KEYS = [
    'meta',
    'alt',
    'shift',
    'control'
];
const isModifierKey = (pressedKey)=>{
    return MODIFIER_KEYS.includes(pressedKey);
};
const getPressedModifierKeys = (event, mergeMetaKeys = false)=>{
    const pressedModifierKeys = [];
    if (event.altKey) {
        pressedModifierKeys.push('alt');
    }
    if (mergeMetaKeys && (event.ctrlKey || event.metaKey)) {
        pressedModifierKeys.push('control/meta');
    } else {
        if (event.ctrlKey) {
            pressedModifierKeys.push('control');
        }
        if (event.metaKey) {
            pressedModifierKeys.push('meta');
        }
    }
    if (event.shiftKey) {
        pressedModifierKeys.push('shift');
    }
    return pressedModifierKeys;
};
const getEventKeyCombinations = (event, platformCheck)=>{
    if (typeof event.key !== 'string') {
        return [];
    }
    const pressedKey = normalizeEventKey(event);
    const modifier = isModifierKey(pressedKey);
    const modifiers = modifier ? [] : getPressedModifierKeys(event);
    const literal = [
        pressedKey
    ].concat(modifiers);
    const combinations = [
        literal
    ];
    if (!modifier) {
        const isMac = typeof platformCheck === 'function' ? platformCheck() : false;
        const hasOsNativeModifier = isMac ? modifiers.includes('meta') : modifiers.includes('control');
        if (hasOsNativeModifier) {
            combinations.push([
                pressedKey
            ].concat(getPressedModifierKeys(event, true)));
        }
    }
    return combinations;
};
