import { useMemo, useCallback } from 'react';
export function useFocusWithin({
  onChildFocus,
  onChildBlur,
  onBlur,
  onFocus
}) {
  const hasBluredChild = useCallback(e => e.currentTarget !== e.relatedTarget && e.currentTarget.contains(e.relatedTarget), []);
  const hasFocusedChild = useCallback(e => e.currentTarget !== document.activeElement && e.currentTarget.contains(document.activeElement), []);
  const handleFocus = useCallback(e => {
    if (hasFocusedChild(e)) {
      onChildFocus?.(e);
    } else {
      onFocus?.(e);
    }
  }, [onChildFocus, onFocus]);
  const handleBlur = useCallback(e => {
    if (hasBluredChild(e)) {
      onChildBlur?.(e);
    } else {
      onBlur?.(e);
    }
  }, [onChildBlur, onBlur]);
  return useMemo(() => ({
    onBlur: handleBlur,
    onFocus: handleFocus,
    hasFocusedChild,
    hasBluredChild
  }), [onChildFocus, onChildBlur, onBlur, onFocus]);
}