'use strict';

var React = require('react');

const callbacks = new Map();
const handleClick = (e) => {
    for (const [ref, callback] of callbacks) {
        if (ref.current && !ref.current.contains(e.target)) {
            callback(e);
        }
    }
};
const initSharedOutSideClickListener = () => {
    document.addEventListener('mousedown', handleClick, {
        passive: true,
    });
};
/** require the use of initSharedOutSideClickListener once, anywhere globally  */
const useSharedOutsideClick = (ref, callback) => {
    React.useEffect(() => {
        callbacks.set(ref, callback);
        return () => {
            callbacks.delete(ref);
        };
    }, [ref, callback]);
};
const useOutsideClick = (ref, callback) => {
    const handleClick = React.useCallback((e) => {
        var _a;
        // If the click target is a descendant of the ref, ignore it
        if ((_a = ref.current) === null || _a === void 0 ? void 0 : _a.contains(e.target)) {
            return;
        }
        // Otherwise, invoke the callback
        callback(e);
    }, []);
    React.useEffect(() => {
        document.addEventListener('mousedown', handleClick, {
            passive: true,
        });
        return () => {
            document.removeEventListener('mousedown', handleClick);
        };
    }, [handleClick]);
};

exports.initSharedOutSideClickListener = initSharedOutSideClickListener;
exports.useOutsideClick = useOutsideClick;
exports.useSharedOutsideClick = useSharedOutsideClick;
