"use strict";

exports.__esModule = true;
exports.default = void 0;
var _noop2 = _interopRequireDefault(require("lodash/fp/noop"));
var _isEmpty2 = _interopRequireDefault(require("lodash/fp/isEmpty"));
var _has2 = _interopRequireDefault(require("lodash/fp/has"));
var _get2 = _interopRequireDefault(require("lodash/fp/get"));
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _uuid = require("uuid");
var _novaIcons = require("@coorpacademy/nova-icons");
var _provider = _interopRequireWildcard(require("../../atom/provider"));
var _tooltip = _interopRequireWildcard(require("../../atom/tooltip"));
var _button = _interopRequireDefault(require("../../atom/button"));
var _favorite = _interopRequireDefault(require("./favorite.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 }; }
const Favorite = ({
  favorite,
  addFavoriteToolTip,
  removeFavoriteToolTip,
  className,
  'aria-label': ariaLabel = {},
  onFavoriteClick = _noop2.default,
  disabled
}, legacyContext) => {
  const skin = (0, _provider.GetSkinFromContext)(legacyContext);
  const translate = (0, _provider.GetTranslateFromContext)(legacyContext);
  const handleFavoriteClick = (0, _react.useCallback)(e => {
    e.stopPropagation();
    e.preventDefault();
    if (!disabled) onFavoriteClick(e);
  }, [disabled, onFavoriteClick]);
  const primaryColor = (0, _get2.default)('common.primary', skin);
  const darkColor = (0, _get2.default)('common.dark', skin);
  const brandColor = (0, _get2.default)('common.brand', skin);
  const buttonRef = (0, _react.useRef)(null);
  const allowToolTip = (0, _react.useMemo)(() => !(0, _isEmpty2.default)(removeFavoriteToolTip) && !(0, _isEmpty2.default)(addFavoriteToolTip), [addFavoriteToolTip, removeFavoriteToolTip]);

  // to be replaced by useId when React17 is bumped to React18
  const [favoriteButtonAnchorId] = (0, _react.useState)(allowToolTip ? (0, _uuid.v5)('engaged-battle-', _uuid.v5.URL) : undefined);
  const toolTipContent = (0, _react.useMemo)(
  // eslint-disable-next-line no-nested-ternary
  () => allowToolTip ? favorite ? removeFavoriteToolTip : addFavoriteToolTip : null, [addFavoriteToolTip, favorite, removeFavoriteToolTip, allowToolTip]);
  const [mouseLeaveTimer, setMouseLeaveTimer] = (0, _react.useState)(undefined);
  const [toolTipIsVisible, setToolTipIsVisible] = (0, _react.useState)(false);
  const handleMouseOver = (0, _react.useCallback)(() => {
    mouseLeaveTimer && clearTimeout(mouseLeaveTimer);
    /* istanbul ignore next */
    (0, _has2.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, _has2.default)(['current', 'blur'], buttonRef) && buttonRef.current.blur();
    }, 500));
  }, []);
  const handleKeyPress = (0, _react.useCallback)(event => {
    (0, _tooltip.toggleStateOnKeyPress)(toolTipIsVisible, setToolTipIsVisible, buttonRef)(event);
  }, [toolTipIsVisible]);
  const iconWrapperClassName = (0, _react.useMemo)(() => (0, _classnames.default)(_favorite.default.favorite, className, favorite && _favorite.default.selected), [className, favorite]);
  const icon = (0, _react.useMemo)(() => favorite ? /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyCheck
  // eslint-disable-next-line css-modules/no-undef-class
  , {
    className: _favorite.default.checkIcon,
    style: {
      color: brandColor
    },
    width: 13,
    height: 13,
    "data-testid": "favorite-check-icon",
    "aria-label": ariaLabel.removeFromFavorite
  }) : /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionNavigationMore, {
    className: _favorite.default.moreIcon,
    style: {
      color: darkColor
    },
    width: 13,
    height: 13,
    "data-testid": "favorite-add-icon",
    "aria-label": ariaLabel.addToFavorite
  }), [ariaLabel.addToFavorite, ariaLabel.removeFromFavorite, brandColor, darkColor, favorite]);
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _favorite.default.favoriteWrapper,
    "data-testid": "favorite-wrapper"
  }, /*#__PURE__*/_react.default.createElement(_button.default, {
    buttonRef: buttonRef,
    type: "button",
    "data-for": favoriteButtonAnchorId,
    "data-tooltip-place": "left",
    "data-tip": allowToolTip,
    "data-name": "favorite",
    "data-testid": "favorite-button",
    "aria-label": ariaLabel.favorite,
    buttonContentClassName: iconWrapperClassName,
    onClick: handleFavoriteClick,
    style: {
      color: primaryColor
    },
    onKeyDown: handleKeyPress,
    onMouseLeave: handleMouseLeave,
    onMouseOver: handleMouseOver,
    tabIndex: 0,
    useButtonTag: true,
    useWrapper: false
  }, icon), /*#__PURE__*/_react.default.createElement(_tooltip.default, {
    fontSize: 12,
    anchorId: favoriteButtonAnchorId,
    toolTipIsVisible: toolTipIsVisible,
    TooltipContent: toolTipContent,
    closeToolTipInformationTextAriaLabel: translate('close_tooltip_information'),
    stickyOnKeyboardNavigation: true
  }));
};
Favorite.propTypes = process.env.NODE_ENV !== "production" ? {
  className: _propTypes.default.string,
  favorite: _propTypes.default.bool,
  disabled: _propTypes.default.bool,
  addFavoriteToolTip: _propTypes.default.string,
  removeFavoriteToolTip: _propTypes.default.string,
  onFavoriteClick: _propTypes.default.func,
  'aria-label': _propTypes.default.shape({
    favorite: _propTypes.default.string,
    addToFavorite: _propTypes.default.string,
    removeFromFavorite: _propTypes.default.string
  })
} : {};
Favorite.contextTypes = {
  skin: _provider.default.childContextTypes.skin,
  translate: _provider.default.childContextTypes.translate
};
var _default = exports.default = Favorite;
//# sourceMappingURL=favorite.js.map