"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
const useEventListener = (eventName, handler, target, option) => {
    const savedHandler = (0, react_1.useRef)();
    (0, react_1.useEffect)(() => {
        savedHandler.current = handler;
    }, [handler]);
    (0, react_1.useEffect)(() => {
        const element = target !== null && target !== void 0 ? target : window;
        const isSupported = Boolean(element && element.addEventListener);
        const eventListener = (event) => { var _a; return (_a = savedHandler.current) === null || _a === void 0 ? void 0 : _a.call(savedHandler, event); };
        if (isSupported) {
            element.addEventListener(eventName, eventListener, option);
        }
        return () => {
            isSupported && element.removeEventListener(eventName, eventListener, option);
        };
    }, [eventName, option, target]);
};
exports.default = useEventListener;
