import { useEffect } from "react";

export default function useOutsideClick({
  enabled,
  refs = [],
  onOutside
}) {
  useEffect(() => {
    if (!enabled) return;
    const handleClickOutside = (event) => {
      const isInside = refs.some((ref) => ref && ref.current && ref.current.contains(event.target));
      if (!isInside) {
        onOutside();
      }
    };
    document.addEventListener("mousedown", handleClickOutside);
    return () => document.removeEventListener("mousedown", handleClickOutside);
  }, [enabled, onOutside, refs]);
}
