function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
import React, { useCallback, useMemo, useState, useRef } from 'react';
import classnames from 'classnames';
import { v5 as uuidV5 } from 'uuid';
import has from 'lodash/fp/has';
import { NovaCompositionNavigationArrowRight as ArrowRight, NovaCompositionCoorpacademyInformationIcon as InformationIcon } from '@coorpacademy/nova-icons';
import ToolTip, { toggleStateOnKeyPress } from '../tooltip';
import Provider, { GetTranslateFromContext } from '../provider';
import propTypes from './prop-types';
import style from './style.css';
const BattleOpponent = ({
  isAlreadyEngaged,
  isRandom,
  userAvatarSrc,
  onClick,
  displayName,
  tooltipText,
  'aria-label': ariaLabel
}, legacyContext) => {
  const translate = GetTranslateFromContext(legacyContext);
  const wrapperClassnames = useMemo(() => classnames(style.card, style.opponentCard, isRandom ? style.random : null), [isRandom]);
  const displayNameClassnames = useMemo(() => classnames(style.name, isAlreadyEngaged ? style.alreadyEngaged : null), [isAlreadyEngaged]);
  const [mouseLeaveTimer, setMouseLeaveTimer] = useState(undefined);
  const [toolTipIsVisible, setToolTipIsVisible] = useState(false);

  // to be replaced by useId when React17 is bumped to React18
  const [battleOpponentInfoId] = useState(isAlreadyEngaged ? uuidV5('engaged-battle-', uuidV5.URL) : undefined);
  const buttonRef = useRef(null);
  const handleMouseOver = useCallback(() => {
    mouseLeaveTimer && clearTimeout(mouseLeaveTimer);
    // @ts-expect-error (error: focus does not exists on type never)
    /* istanbul ignore next */
    has(['current', 'focus'], buttonRef) && buttonRef.current.focus();
    setToolTipIsVisible(true);
  }, [mouseLeaveTimer]);
  const handleMouseLeave = useCallback(() => {
    setMouseLeaveTimer(setTimeout(() => {
      setToolTipIsVisible(false);
      // @ts-expect-error (error: blur does not exists on type never)
      /* istanbul ignore next */
      has(['current', 'blur'], buttonRef) && buttonRef.current.blur();
    }, 500));
  }, []);
  const handleKeyPress = useCallback(event => {
    toggleStateOnKeyPress(toolTipIsVisible, setToolTipIsVisible, buttonRef)(event);
  }, [toolTipIsVisible]);
  const TooltipContent = useCallback(_props => /*#__PURE__*/React.createElement("p", _extends({}, _props, {
    onMouseOver: handleMouseOver,
    onMouseLeave: handleMouseLeave,
    "data-testid": "battle-opponent-tooltip-content"
  }), tooltipText), [handleMouseLeave, handleMouseOver, tooltipText]);
  return /*#__PURE__*/React.createElement("li", _extends({}, !isAlreadyEngaged && {
    onClick
  }, {
    className: wrapperClassnames,
    "data-testid": "battle-opponent-wrapper",
    tabIndex: 0
  }), /*#__PURE__*/React.createElement("div", {
    className: style.avatar
  }, isRandom ? null : /*#__PURE__*/React.createElement("img", {
    src: userAvatarSrc,
    alt: ""
  })), /*#__PURE__*/React.createElement("p", {
    className: displayNameClassnames,
    tabIndex: 0
  }, displayName), isAlreadyEngaged ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("button", {
    ref: buttonRef,
    tabIndex: 0,
    type: "button",
    className: style.tooltipIconContainer,
    "data-testid": "battle-engaged-opponent-information-button",
    onKeyDown: handleKeyPress,
    onMouseLeave: handleMouseLeave,
    onMouseOver: handleMouseOver,
    "data-for": battleOpponentInfoId,
    "data-tooltip-place": "left",
    "data-tip": isAlreadyEngaged,
    "aria-label": ariaLabel
  }, /*#__PURE__*/React.createElement(InformationIcon, {
    className: style.informationIcon,
    width: 20,
    height: 20
  })), /*#__PURE__*/React.createElement("span", {
    className: style.toolTipWrapper
  }, /*#__PURE__*/React.createElement(ToolTip, {
    fontSize: 12,
    anchorId: battleOpponentInfoId,
    toolTipIsVisible: toolTipIsVisible,
    TooltipContent: TooltipContent,
    closeToolTipInformationTextAriaLabel: translate('close_tooltip_information')
  }))) : /*#__PURE__*/React.createElement(ArrowRight, {
    className: style.rightArrow,
    width: 16,
    height: 16
  }));
};
BattleOpponent.propTypes = process.env.NODE_ENV !== "production" ? propTypes : {};
BattleOpponent.contextTypes = {
  translate: Provider.childContextTypes.translate
};
export default BattleOpponent;
//# sourceMappingURL=index.js.map