"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useKeyboardShortcut = exports.useKeyDown = exports.useKeyUp = void 0;
const react_1 = require("react");
const isMac_1 = require("../../utils/isMac");
class TempEvent {
    constructor(target, ev) {
        this.current = target;
        this.currentTarget = target;
        this.keyboardEvent = ev;
    }
    preventDefault() { }
    stopPropagation() { }
}
const useKeyUp = (handler, keys, ref) => {
    (0, react_1.useEffect)(() => {
        const keyHandler = (e) => {
            if (keys && keys.find((k) => keyMatch(k, e))) {
                e.preventDefault();
                e.stopPropagation();
                const event = new TempEvent(ref ? ref.current : e.target, e);
                handler(event);
            }
            else if (!keys) {
                const event = new TempEvent(ref ? ref.current : e.target, e);
                handler(event);
            }
        };
        document.addEventListener('keyup', keyHandler);
        return () => {
            document.removeEventListener('keyup', keyHandler);
        };
    }, [handler, ref]);
};
exports.useKeyUp = useKeyUp;
const keyMatch = (k, e) => {
    if (k === 'esc') {
        k = 'escape';
    }
    return k === e.key || k.toLowerCase() === e.key.toLocaleLowerCase();
};
const useKeyDown = (handler, keys, ref) => {
    (0, react_1.useEffect)(() => {
        const keyHandler = (e) => {
            if (keys && keys.find((k) => keyMatch(k, e))) {
                e.preventDefault();
                e.stopPropagation();
                const event = new TempEvent(ref ? ref.current : e.target, e);
                handler(event);
            }
            else if (!keys) {
                const event = new TempEvent(ref ? ref.current : e.target, e);
                handler(event);
            }
        };
        document.addEventListener('keydown', keyHandler);
        return () => {
            document.removeEventListener('keydown', keyHandler);
        };
    }, [handler]);
};
exports.useKeyDown = useKeyDown;
const metaKeysMatch = (event, hotkey) => {
    for (const key in hotkey.meta) {
        if (hotkey.meta[key] !== event[key]) {
            return false;
        }
    }
    return true;
};
const useKeyboardShortcut = (key, handler, ref, event = 'keyDown') => {
    const hotKey = (0, react_1.useMemo)(() => {
        const keys = key.toLowerCase().split('+');
        const hotKey = {
            meta: {
                ctrlKey: false,
                shiftKey: false,
                altKey: false,
                metaKey: false,
            },
        };
        // ⌘
        for (const k of keys) {
            if (k === 'cmd') {
                if ((0, isMac_1.isMac)()) {
                    hotKey.meta.metaKey = true;
                }
                else {
                    hotKey.meta.ctrlKey = true;
                }
            }
            else if (k === 'alt') {
                hotKey.meta.altKey = true;
            }
            else if (k === 'shift') {
                hotKey.meta.shiftKey = true;
            }
            else {
                hotKey.key = k;
            }
        }
        return hotKey;
    }, [key]);
    if (event === 'keyUp') {
        return (0, exports.useKeyUp)((e) => {
            if (keyMatch(hotKey.key, e.keyboardEvent) &&
                metaKeysMatch(e.keyboardEvent, hotKey)) {
                e.keyboardEvent.stopPropagation();
                e.keyboardEvent.preventDefault();
                handler(e);
            }
        }, undefined, ref);
    }
    else if (event === 'keyDown') {
        return (0, exports.useKeyDown)((e) => {
            if (keyMatch(hotKey.key, e.keyboardEvent) &&
                metaKeysMatch(e.keyboardEvent, hotKey)) {
                e.keyboardEvent.stopPropagation();
                e.keyboardEvent.preventDefault();
                handler(e);
            }
        }, undefined, ref);
    }
};
exports.useKeyboardShortcut = useKeyboardShortcut;
//# sourceMappingURL=index.js.map