"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useEventListener = void 0;
var react_1 = require("react");
var events = {
    'keydown.arrowdown': {
        event: 'keydown',
        fn: function (event, callback) {
            if (event.keyCode === 40) {
                callback(event);
            }
        },
    },
    'keydown.arrowup': {
        event: 'keydown',
        fn: function (event, callback) {
            if (event.keyCode === 38) {
                callback(event);
            }
        },
    },
    'keydown.enter': {
        event: 'keydown',
        fn: function (event, callback) {
            if (event.keyCode === 13 || event.code === 'Enter' || event.code === 'NumpadEnter') {
                callback(event);
            }
        },
    },
    'keydown.tab': {
        event: 'keydown',
        fn: function (event, callback) {
            if (event.keyCode === 9) {
                callback(event);
            }
        },
    },
};
function useEventListener(eventName, handler, element) {
    var savedHandler = react_1.useRef();
    react_1.useEffect(function () {
        savedHandler.current = handler;
    }, [handler]);
    react_1.useEffect(function () {
        var el = element && element.current ? element.current : window;
        var isSupported = el && el.addEventListener;
        if (!isSupported)
            return;
        if (events[eventName]) {
            var eventListener_1 = function (event) { return events[eventName].fn(event, savedHandler.current); };
            el.addEventListener(events[eventName].event, eventListener_1);
            return function () {
                el.removeEventListener(events[eventName].event, eventListener_1);
            };
        }
        var eventListener = function (event) { return savedHandler.current(event); };
        el.addEventListener(eventName, eventListener);
        return function () {
            el.removeEventListener(eventName, eventListener);
        };
    }, [eventName, element]);
}
exports.useEventListener = useEventListener;
//# sourceMappingURL=useEventListener.js.map