import { useRef, useEffect } from 'react';
import { TargetEventHandlers } from './TargetEventHandlers';
import { normalizeEventOptions } from './normalize-event-options';
import { eventOptionsKey } from './event-option-key';
export var targetMap = new WeakMap();
export function addEventListener(target, eventName, listener, options) {
    if (!targetMap.has(target)) {
        targetMap.set(target, new TargetEventHandlers(target));
    }
    var normalizedEventOptions = normalizeEventOptions(options);
    return targetMap.get(target).add(eventName, listener, normalizedEventOptions);
}
export function useEventHandler(target, eventName, listener, options) {
    var callbackRef = useRef(listener);
    callbackRef.current = listener;
    var optionKey = eventOptionsKey(normalizeEventOptions(options));
    useEffect(function () {
        function cb(e) {
            callbackRef.current(e);
        }
        return addEventListener(target, eventName, cb, options);
    }, [target, eventName, optionKey]);
}
export var EventHandler = function (_a) {
    var target = _a.target, eventName = _a.eventName, listener = _a.listener, options = _a.options;
    useEventHandler(target, eventName, listener, options);
    return null;
};
