import { jsx as _jsx } from "react/jsx-runtime";
// (C) 2007-2025 GoodData Corporation
import { useCallback, useEffect, useRef } from "react";
export function OutsideClickHandler({ onOutsideClick, toggler, useCapture = true, children, }) {
    // Set to true by default so that a `stopPropagation` in the
    // children will not prevent all outside click handlers from firing
    const wrapperElRef = useRef(null);
    const handleClick = useCallback((e) => {
        if (!wrapperElRef.current) {
            // In IE11 the wrapperEl is not initialized for some reason.
            return;
        }
        const target = e.target;
        if (wrapperElRef.current.contains(target) || toggler?.contains(target)) {
            return;
        }
        if (onOutsideClick) {
            onOutsideClick(e);
        }
    }, [onOutsideClick, toggler]);
    const addListeners = useCallback(() => {
        document.addEventListener("mousedown", handleClick, useCapture);
    }, [handleClick, useCapture]);
    const removeListeners = useCallback(() => {
        document.removeEventListener("mousedown", handleClick, useCapture);
    }, [handleClick, useCapture]);
    useEffect(() => {
        addListeners();
        return removeListeners;
    }, [onOutsideClick, useCapture, addListeners, removeListeners]);
    return _jsx("div", { ref: wrapperElRef, children: children });
}
