"use strict";

exports.__esModule = true;
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _uuid = require("uuid");
var _has = _interopRequireDefault(require("lodash/fp/has"));
var _novaIcons = require("@coorpacademy/nova-icons");
var _tooltip = _interopRequireWildcard(require("../tooltip"));
var _provider = _interopRequireWildcard(require("../provider"));
var _propTypes = _interopRequireDefault(require("./prop-types"));
var _style = _interopRequireDefault(require("./style.css"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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); }
const BattleOpponent = ({
  isAlreadyEngaged,
  isRandom,
  userAvatarSrc,
  onClick,
  displayName,
  tooltipText,
  'aria-label': ariaLabel
}, legacyContext) => {
  const translate = (0, _provider.GetTranslateFromContext)(legacyContext);
  const wrapperClassnames = (0, _react.useMemo)(() => (0, _classnames.default)(_style.default.card, _style.default.opponentCard, isRandom ? _style.default.random : null), [isRandom]);
  const displayNameClassnames = (0, _react.useMemo)(() => (0, _classnames.default)(_style.default.name, isAlreadyEngaged ? _style.default.alreadyEngaged : null), [isAlreadyEngaged]);
  const [mouseLeaveTimer, setMouseLeaveTimer] = (0, _react.useState)(undefined);
  const [toolTipIsVisible, setToolTipIsVisible] = (0, _react.useState)(false);

  // to be replaced by useId when React17 is bumped to React18
  const [battleOpponentInfoId] = (0, _react.useState)(isAlreadyEngaged ? (0, _uuid.v5)('engaged-battle-', _uuid.v5.URL) : undefined);
  const buttonRef = (0, _react.useRef)(null);
  const handleMouseOver = (0, _react.useCallback)(() => {
    mouseLeaveTimer && clearTimeout(mouseLeaveTimer);
    // @ts-expect-error (error: focus does not exists on type never)
    /* istanbul ignore next */
    (0, _has.default)(['current', 'focus'], buttonRef) && buttonRef.current.focus();
    setToolTipIsVisible(true);
  }, [mouseLeaveTimer]);
  const handleMouseLeave = (0, _react.useCallback)(() => {
    setMouseLeaveTimer(setTimeout(() => {
      setToolTipIsVisible(false);
      // @ts-expect-error (error: blur does not exists on type never)
      /* istanbul ignore next */
      (0, _has.default)(['current', 'blur'], buttonRef) && buttonRef.current.blur();
    }, 500));
  }, []);
  const handleKeyPress = (0, _react.useCallback)(event => {
    (0, _tooltip.toggleStateOnKeyPress)(toolTipIsVisible, setToolTipIsVisible, buttonRef)(event);
  }, [toolTipIsVisible]);
  const TooltipContent = (0, _react.useCallback)(_props => /*#__PURE__*/_react.default.createElement("p", _extends({}, _props, {
    onMouseOver: handleMouseOver,
    onMouseLeave: handleMouseLeave,
    "data-testid": "battle-opponent-tooltip-content"
  }), tooltipText), [handleMouseLeave, handleMouseOver, tooltipText]);
  return /*#__PURE__*/_react.default.createElement("li", _extends({}, !isAlreadyEngaged && {
    onClick
  }, {
    className: wrapperClassnames,
    "data-testid": "battle-opponent-wrapper",
    tabIndex: 0
  }), /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.avatar
  }, isRandom ? null : /*#__PURE__*/_react.default.createElement("img", {
    src: userAvatarSrc,
    alt: ""
  })), /*#__PURE__*/_react.default.createElement("p", {
    className: displayNameClassnames,
    tabIndex: 0
  }, displayName), isAlreadyEngaged ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("button", {
    ref: buttonRef,
    tabIndex: 0,
    type: "button",
    className: _style.default.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.default.createElement(_novaIcons.NovaCompositionCoorpacademyInformationIcon, {
    className: _style.default.informationIcon,
    width: 20,
    height: 20
  })), /*#__PURE__*/_react.default.createElement("span", {
    className: _style.default.toolTipWrapper
  }, /*#__PURE__*/_react.default.createElement(_tooltip.default, {
    fontSize: 12,
    anchorId: battleOpponentInfoId,
    toolTipIsVisible: toolTipIsVisible,
    TooltipContent: TooltipContent,
    closeToolTipInformationTextAriaLabel: translate('close_tooltip_information')
  }))) : /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionNavigationArrowRight, {
    className: _style.default.rightArrow,
    width: 16,
    height: 16
  }));
};
BattleOpponent.propTypes = process.env.NODE_ENV !== "production" ? _propTypes.default : {};
BattleOpponent.contextTypes = {
  translate: _provider.default.childContextTypes.translate
};
var _default = exports.default = BattleOpponent;
//# sourceMappingURL=index.js.map