import { useRef, useEffect } from 'react';

function useOnClickOutside(onClickOutside) {
    const containerRef = useRef(null);
    useEffect(() => {
        const listener = (event) => {
            const el = containerRef === null || containerRef === void 0 ? void 0 : containerRef.current;
            if (!el || el.contains((event === null || event === void 0 ? void 0 : event.target) || null)) {
                return;
            }
            onClickOutside(event);
        };
        document.addEventListener(`mousedown`, listener);
        document.addEventListener(`touchstart`, listener);
        return () => {
            document.removeEventListener(`mousedown`, listener);
            document.removeEventListener(`touchstart`, listener);
        };
    }, [containerRef, onClickOutside]);
    return { containerRef };
}

export { useOnClickOutside };
