"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useFocusWithin = useFocusWithin;
var _react = require("react");
function useFocusWithin({
  onChildFocus,
  onChildBlur,
  onBlur,
  onFocus
}) {
  const hasBluredChild = (0, _react.useCallback)(e => e.currentTarget !== e.relatedTarget && e.currentTarget.contains(e.relatedTarget), []);
  const hasFocusedChild = (0, _react.useCallback)(e => e.currentTarget !== document.activeElement && e.currentTarget.contains(document.activeElement), []);
  const handleFocus = (0, _react.useCallback)(e => {
    if (hasFocusedChild(e)) {
      onChildFocus?.(e);
    } else {
      onFocus?.(e);
    }
  }, [onChildFocus, onFocus]);
  const handleBlur = (0, _react.useCallback)(e => {
    if (hasBluredChild(e)) {
      onChildBlur?.(e);
    } else {
      onBlur?.(e);
    }
  }, [onChildBlur, onBlur]);
  return (0, _react.useMemo)(() => ({
    onBlur: handleBlur,
    onFocus: handleFocus,
    hasFocusedChild,
    hasBluredChild
  }), [onChildFocus, onChildBlur, onBlur, onFocus]);
}