"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useDropdownTrigger = void 0;
var _react = require("react");
const useDropdownTrigger = ({
  onMouseEnter,
  onMouseLeave,
  onClick,
  triggerOnClickHandler,
  isOpen
}) => {
  // used to determine if mouse is being used
  // some accessibility tools like NVDA will send onClick event instead of onKeyPress when used with keyboard
  const isMouse = (0, _react.useRef)(false);
  const onClickCallback = (0, _react.useCallback)(event => {
    onClick?.(event, !isMouse.current);
    if (typeof triggerOnClickHandler === 'function') {
      triggerOnClickHandler({
        isOpen
      }, !isMouse.current);
    }
  }, [isOpen]);
  const onMouseEnterFn = e => {
    isMouse.current = true;
    onMouseEnter?.(e);
  };
  const onMouseLeaveFn = e => {
    isMouse.current = false;
    onMouseLeave?.(e);
  };
  const onMouseClick = event => {
    event.stopPropagation();
    onClickCallback(event);
  };
  const onKeyDown = event => {
    if (event.key === 'Enter' || event.key === ' ') {
      event.stopPropagation();
      event.preventDefault();
      onClickCallback(event);
    }
  };
  return {
    onMouseEnter: onMouseEnterFn,
    onMouseLeave: onMouseLeaveFn,
    onMouseClick,
    onKeyDown
  };
};
exports.useDropdownTrigger = useDropdownTrigger;