"use client";
'use strict';

var P7GR5CS5 = require('./P7GR5CS5.cjs');
var LMDWO4NN = require('./LMDWO4NN.cjs');
var ABQUS43J = require('./ABQUS43J.cjs');
var _3YLGPPWQ = require('./3YLGPPWQ.cjs');
var DTR5TSDJ = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/__chunks/DTR5TSDJ.cjs');
var focus = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/utils/focus.cjs');
var PBFD2E7P = require('../../../../../../@ariakit_core@0.4.15/node_modules/@ariakit/core/esm/__chunks/PBFD2E7P.cjs');
var React = require('react');

var TagName = "div";
function getMouseDestination(event) {
  const relatedTarget = event.relatedTarget;
  if ((relatedTarget == null ? void 0 : relatedTarget.nodeType) === Node.ELEMENT_NODE) {
    return relatedTarget;
  }
  return null;
}
function hoveringInside(event) {
  const nextElement = getMouseDestination(event);
  if (!nextElement) return false;
  return DTR5TSDJ.contains(event.currentTarget, nextElement);
}
var symbol = Symbol("composite-hover");
function movingToAnotherItem(event) {
  let dest = getMouseDestination(event);
  if (!dest) return false;
  do {
    if (PBFD2E7P.hasOwnProperty(dest, symbol) && dest[symbol]) return true;
    dest = dest.parentElement;
  } while (dest);
  return false;
}
var useCompositeHover = LMDWO4NN.createHook(
  function useCompositeHover2(_a) {
    var _b = _a, {
      store,
      focusOnHover = true,
      blurOnHoverEnd = !!focusOnHover
    } = _b, props = _3YLGPPWQ.__objRest(_b, [
      "store",
      "focusOnHover",
      "blurOnHoverEnd"
    ]);
    const context = P7GR5CS5.useCompositeContext();
    store = store || context;
    PBFD2E7P.invariant(
      store,
      process.env.NODE_ENV !== "production" && "CompositeHover must be wrapped in a Composite component."
    );
    const isMouseMoving = ABQUS43J.useIsMouseMoving();
    const onMouseMoveProp = props.onMouseMove;
    const focusOnHoverProp = ABQUS43J.useBooleanEvent(focusOnHover);
    const onMouseMove = ABQUS43J.useEvent((event) => {
      onMouseMoveProp == null ? void 0 : onMouseMoveProp(event);
      if (event.defaultPrevented) return;
      if (!isMouseMoving()) return;
      if (!focusOnHoverProp(event)) return;
      if (!focus.hasFocusWithin(event.currentTarget)) {
        const baseElement = store == null ? void 0 : store.getState().baseElement;
        if (baseElement && !focus.hasFocus(baseElement)) {
          baseElement.focus();
        }
      }
      store == null ? void 0 : store.setActiveId(event.currentTarget.id);
    });
    const onMouseLeaveProp = props.onMouseLeave;
    const blurOnHoverEndProp = ABQUS43J.useBooleanEvent(blurOnHoverEnd);
    const onMouseLeave = ABQUS43J.useEvent((event) => {
      var _a2;
      onMouseLeaveProp == null ? void 0 : onMouseLeaveProp(event);
      if (event.defaultPrevented) return;
      if (!isMouseMoving()) return;
      if (hoveringInside(event)) return;
      if (movingToAnotherItem(event)) return;
      if (!focusOnHoverProp(event)) return;
      if (!blurOnHoverEndProp(event)) return;
      store == null ? void 0 : store.setActiveId(null);
      (_a2 = store == null ? void 0 : store.getState().baseElement) == null ? void 0 : _a2.focus();
    });
    const ref = React.useCallback((element) => {
      if (!element) return;
      element[symbol] = true;
    }, []);
    props = _3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({}, props), {
      ref: ABQUS43J.useMergeRefs(ref, props.ref),
      onMouseMove,
      onMouseLeave
    });
    return PBFD2E7P.removeUndefinedValues(props);
  }
);
LMDWO4NN.memo(
  LMDWO4NN.forwardRef(function CompositeHover2(props) {
    const htmlProps = useCompositeHover(props);
    return LMDWO4NN.createElement(TagName, htmlProps);
  })
);

exports.useCompositeHover = useCompositeHover;
//# sourceMappingURL=UQQRIHDV.cjs.map
