'use strict';

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

var TagName = "a";
var useHovercardAnchor = LMDWO4NN.createHook(
  function useHovercardAnchor2(_a) {
    var _b = _a, { store, showOnHover = true } = _b, props = _3YLGPPWQ.__objRest(_b, ["store", "showOnHover"]);
    const context = EM5CXX6A.useHovercardProviderContext();
    store = store || context;
    PBFD2E7P.invariant(
      store,
      process.env.NODE_ENV !== "production" && "HovercardAnchor must receive a `store` prop or be wrapped in a HovercardProvider component."
    );
    const disabled = PBFD2E7P.disabledFromProps(props);
    const showTimeoutRef = React.useRef(0);
    React.useEffect(() => () => window.clearTimeout(showTimeoutRef.current), []);
    React.useEffect(() => {
      const onMouseLeave = (event) => {
        if (!store) return;
        const { anchorElement } = store.getState();
        if (!anchorElement) return;
        if (event.target !== anchorElement) return;
        window.clearTimeout(showTimeoutRef.current);
        showTimeoutRef.current = 0;
      };
      return events.addGlobalEventListener("mouseleave", onMouseLeave, true);
    }, [store]);
    const onMouseMoveProp = props.onMouseMove;
    const showOnHoverProp = ABQUS43J.useBooleanEvent(showOnHover);
    const isMouseMoving = ABQUS43J.useIsMouseMoving();
    const onMouseMove = ABQUS43J.useEvent((event) => {
      onMouseMoveProp == null ? void 0 : onMouseMoveProp(event);
      if (disabled) return;
      if (!store) return;
      if (event.defaultPrevented) return;
      if (showTimeoutRef.current) return;
      if (!isMouseMoving()) return;
      if (!showOnHoverProp(event)) return;
      const element = event.currentTarget;
      store.setAnchorElement(element);
      store.setDisclosureElement(element);
      const { showTimeout, timeout } = store.getState();
      const showHovercard = () => {
        showTimeoutRef.current = 0;
        if (!isMouseMoving()) return;
        store == null ? void 0 : store.setAnchorElement(element);
        store == null ? void 0 : store.show();
        queueMicrotask(() => {
          store == null ? void 0 : store.setDisclosureElement(element);
        });
      };
      const timeoutMs = showTimeout != null ? showTimeout : timeout;
      if (timeoutMs === 0) {
        showHovercard();
      } else {
        showTimeoutRef.current = window.setTimeout(showHovercard, timeoutMs);
      }
    });
    const onClickProp = props.onClick;
    const onClick = ABQUS43J.useEvent((event) => {
      onClickProp == null ? void 0 : onClickProp(event);
      if (!store) return;
      window.clearTimeout(showTimeoutRef.current);
      showTimeoutRef.current = 0;
    });
    const ref = React.useCallback(
      (element) => {
        if (!store) return;
        const { anchorElement } = store.getState();
        if (anchorElement == null ? void 0 : anchorElement.isConnected) return;
        store.setAnchorElement(element);
      },
      [store]
    );
    props = _3YLGPPWQ.__spreadProps(_3YLGPPWQ.__spreadValues({}, props), {
      ref: ABQUS43J.useMergeRefs(ref, props.ref),
      onMouseMove,
      onClick
    });
    props = PFRGBC2K.useFocusable(props);
    return props;
  }
);
LMDWO4NN.forwardRef(function HovercardAnchor2(props) {
  const htmlProps = useHovercardAnchor(props);
  return LMDWO4NN.createElement(TagName, htmlProps);
});

exports.useHovercardAnchor = useHovercardAnchor;
//# sourceMappingURL=MR26YJYP.cjs.map
