"use strict";

exports.__esModule = true;
exports.default = void 0;
var _noop2 = _interopRequireDefault(require("lodash/fp/noop"));
var _get2 = _interopRequireDefault(require("lodash/fp/get"));
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _uuid = require("uuid");
var _novaIcons = require("@coorpacademy/nova-icons");
var _provider = _interopRequireWildcard(require("../../atom/provider"));
var _tooltip = _interopRequireWildcard(require("../../atom/tooltip"));
var _link = _interopRequireDefault(require("../../atom/link"));
var _engineStars = _interopRequireDefault(require("./engine-stars.css"));
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 _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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 ICONS = {
  microlearning: _novaIcons.NovaCompositionCoorpacademyTimer,
  learner: _novaIcons.NovaSolidContentContentBook1,
  battle: _novaIcons.NovaCompositionCoorpacademyBolt,
  certifications: _novaIcons.NovaSolidSchoolScienceGraduationHat,
  bonus: _novaIcons.NovaSolidVoteRewardsRewardsBadge1,
  article: _novaIcons.NovaCompositionCoorpacademyArticle,
  scorm: _novaIcons.NovaCompositionCoorpacademyScorm,
  video: _novaIcons.NovaCompositionCoorpacademyVideo,
  podcast: _novaIcons.NovaCompositionCoorpacademyMicrophone,
  review: _novaIcons.NovaCompositionCoorpacademyRevision
};
const ToolTipContent = ({
  preMessage,
  linkMessage,
  endMessage,
  onClick,
  color,
  handleContentMouseOver
}) => /*#__PURE__*/_react.default.createElement("p", {
  className: _engineStars.default.toolTipContent,
  onMouseOver: handleContentMouseOver,
  "data-testid": "react-tooltip-content"
}, /*#__PURE__*/_react.default.createElement("span", null, preMessage), /*#__PURE__*/_react.default.createElement(_link.default, {
  onClick: onClick,
  style: {
    color
  }
}, linkMessage), /*#__PURE__*/_react.default.createElement("span", null, endMessage));
ToolTipContent.propTypes = process.env.NODE_ENV !== "production" ? {
  preMessage: _propTypes.default.string,
  linkMessage: _propTypes.default.string,
  endMessage: _propTypes.default.string,
  color: _propTypes.default.string,
  onClick: _propTypes.default.func,
  handleContentMouseOver: _propTypes.default.func
} : {};
const EngineStars = (props, legacyContext) => {
  const skin = (0, _provider.GetSkinFromContext)(legacyContext);
  const translate = (0, _provider.GetTranslateFromContext)(legacyContext);
  const {
    toolTip = null,
    disabled,
    type,
    stars,
    title,
    active = false,
    onClick = _noop2.default,
    className
  } = props;
  const handleClick = e => {
    e.stopPropagation();
    e.preventDefault();
    onClick(e);
  };
  const dark = (0, _get2.default)('common.dark', skin);
  const light = (0, _get2.default)('common.light', skin);
  const IconType = ICONS[type];
  const primary = (0, _get2.default)('common.primary', skin);

  // to replace by useId when React17 is bumped to React18
  const [engineStarsContentId] = (0, _react.useState)(disabled ? (0, _uuid.v5)('engine-stars', _uuid.v5.URL) : undefined);
  const [toolTipIsVisible, setToolTipIsVisible] = (0, _react.useState)(false);
  const [mouseLeaveTimer, setMouseLeaveTimer] = (0, _react.useState)(undefined);
  const buttonRef = (0, _react.useRef)(null);
  const handleKeyPress = (0, _react.useCallback)(event => {
    (0, _tooltip.toggleStateOnKeyPress)(toolTipIsVisible, setToolTipIsVisible, buttonRef)(event);
  }, [toolTipIsVisible]);
  const handleMouseOver = (0, _react.useCallback)(() => {
    mouseLeaveTimer && clearTimeout(mouseLeaveTimer);
    setToolTipIsVisible(true);
  }, [mouseLeaveTimer]);
  const handleContentMouseOver = (0, _react.useCallback)(() => {
    mouseLeaveTimer && /* istanbul ignore next */clearTimeout(mouseLeaveTimer);
  }, [mouseLeaveTimer]);
  const handleMouseLeave = (0, _react.useCallback)(() => {
    setMouseLeaveTimer(setTimeout(() => setToolTipIsVisible(false), 500));
  }, []);
  const toolTipContentProps = (0, _react.useMemo)(() => toolTip ? {
    preMessage: toolTip.preMessage,
    linkMessage: toolTip.linkMessage,
    endMessage: toolTip.endMessage,
    color: primary,
    onClick: toolTip.onClick
  } : null, [primary, toolTip]);
  const toolTipProps = {
    closeToolTipInformationTextAriaLabel: translate('close_tooltip_information')
  };
  const TooltipContent = (0, _react.useCallback)(_props => /*#__PURE__*/_react.default.createElement(ToolTipContent, _extends({}, toolTipContentProps, _props, {
    handleContentMouseOver: handleContentMouseOver
  })), [handleContentMouseOver, toolTipContentProps]);
  return /*#__PURE__*/_react.default.createElement("button", {
    ref: buttonRef,
    "data-tip": disabled,
    "data-engine": type,
    "data-testid": `engine-stars-${type}`
    // eslint-disable-next-line no-nested-ternary
    ,
    onClick: disabled ? handleKeyPress : active ? _noop2.default : handleClick,
    "data-for": engineStarsContentId,
    className: (0, _classnames.default)([_engineStars.default.engineStars, disabled ? _engineStars.default.disabled : '', active ? _engineStars.default.active : '', onClick !== _noop2.default ? _engineStars.default.clickable : null, className]),
    type: "button",
    "data-tooltip-place": "left",
    onKeyDown: handleKeyPress,
    onMouseLeave: handleMouseLeave,
    onMouseOver: handleMouseOver,
    "aria-label": `${title} - ${stars} ${translate('stars')}`
  }, disabled ? /*#__PURE__*/_react.default.createElement(_tooltip.default, _extends({}, toolTipProps, {
    anchorId: engineStarsContentId,
    toolTipIsVisible: toolTipIsVisible,
    TooltipContent: TooltipContent,
    mouseLeaveTimer: mouseLeaveTimer
  })) : null, /*#__PURE__*/_react.default.createElement("span", {
    className: _engineStars.default.engineIcon,
    style: {
      backgroundColor: onClick === _noop2.default ? light : primary
    }
  }, /*#__PURE__*/_react.default.createElement(IconType, {
    className: _engineStars.default.iconHeader,
    width: "30"
  })), /*#__PURE__*/_react.default.createElement("div", {
    className: _engineStars.default.score,
    style: {
      color: active ? primary : dark
    }
  }, /*#__PURE__*/_react.default.createElement("p", {
    "data-name": "star-counter"
  }, stars), /*#__PURE__*/_react.default.createElement("span", null, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyStar, {
    className: _engineStars.default.iconStar,
    style: {
      color: active ? primary : dark
    }
  }))), /*#__PURE__*/_react.default.createElement("div", {
    className: _engineStars.default.scoreTitle
  }, title));
};
EngineStars.propTypes = process.env.NODE_ENV !== "production" ? {
  type: _propTypes.default.string.isRequired,
  stars: _propTypes.default.number.isRequired,
  title: _propTypes.default.string.isRequired,
  active: _propTypes.default.bool,
  disabled: _propTypes.default.bool,
  onClick: _propTypes.default.func,
  className: _propTypes.default.string,
  toolTip: _propTypes.default.shape({
    preMessage: _propTypes.default.string,
    linkMessage: _propTypes.default.string,
    endMessage: _propTypes.default.string,
    onClick: _propTypes.default.func
  })
} : {};
EngineStars.contextTypes = {
  skin: _provider.default.childContextTypes.skin,
  translate: _provider.default.childContextTypes.translate
};
var _default = exports.default = EngineStars;
//# sourceMappingURL=engine-stars.js.map