"use strict";

exports.__esModule = true;
exports.toggleStateOnKeyPress = exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactTooltip = _interopRequireDefault(require("react-tooltip"));
var _isString = _interopRequireDefault(require("lodash/fp/isString"));
var _has = _interopRequireDefault(require("lodash/fp/has"));
var _isNil = _interopRequireDefault(require("lodash/fp/isNil"));
var _keys = _interopRequireDefault(require("lodash/fp/keys"));
var _novaIcons = require("@coorpacademy/nova-icons");
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; }
const FontSizes = {
  12: _style.default.tooltipContentFontSize12,
  14: _style.default.tooltipContentFontSize14
};
const IconSizes = {
  small: 12,
  big: 20
};
const ToolTipWrapper = ({
  toolTipIsVisible,
  anchorId,
  closeToolTipInformationTextAriaLabel,
  content,
  handleContentMouseOver,
  iconSize,
  tooltipClassName,
  usesAnchorElement,
  placement
}) => {
  const coorpToolTipClasses = (0, _classnames.default)(_style.default.toolTip, tooltipClassName,
  // eslint-disable-next-line no-nested-ternary
  usesAnchorElement ? null : iconSize === 'big' ? _style.default.bigIconToolTip : _style.default.smallIconToolTip);
  if (!toolTipIsVisible) return null;
  if (anchorId) {
    return /*#__PURE__*/_react.default.createElement(_reactTooltip.default, {
      id: anchorId,
      className: _style.default.toolTipReact,
      "data-event-off": "click",
      place: placement,
      effect: "solid",
      "aria-label": closeToolTipInformationTextAriaLabel
    }, content);
  } else {
    return /*#__PURE__*/_react.default.createElement("div", {
      className: coorpToolTipClasses,
      "data-testid": "tooltip",
      "aria-label": closeToolTipInformationTextAriaLabel,
      onMouseOver: handleContentMouseOver
    }, content);
  }
};
ToolTipWrapper.propTypes = process.env.NODE_ENV !== "production" ? {
  toolTipIsVisible: _propTypes.default.bool,
  anchorId: _propTypes.default.string,
  closeToolTipInformationTextAriaLabel: _propTypes.default.string.isRequired,
  content: _propTypes.default.node,
  handleContentMouseOver: _propTypes.default.func,
  iconSize: _propTypes.default.oneOf((0, _keys.default)(IconSizes)),
  tooltipClassName: _propTypes.default.string,
  usesAnchorElement: _propTypes.default.bool
} : {};
const toggleStateOnKeyPress = (state, setState, ref) => event => {
  if (!(0, _has.default)('key', event)) return;else if (event.key === 'Enter') {
    if (ref) ref.current.focus();
    event.stopPropagation();
    event.preventDefault();
    setState(!state);
  } else if (event.key === 'Tab' || event.key === 'Escape') {
    setState(false);
  }
};
exports.toggleStateOnKeyPress = toggleStateOnKeyPress;
const ToolTip = ({
  anchorId,
  TooltipContent,
  'aria-label': ariaLabel,
  'data-testid': dataTestId,
  closeToolTipInformationTextAriaLabel,
  toolTipIsVisible: _toolTipIsVisible,
  iconContainerClassName,
  delayHide = 250,
  fontSize = 14,
  iconSize = 'small',
  AnchorElement,
  tooltipClassName,
  placement = 'left'
}) => {
  const isComponent = (0, _react.useMemo)(() => !(0, _isString.default)(TooltipContent) && /*#__PURE__*/(0, _react.isValidElement)(TooltipContent()), [TooltipContent]);
  const [toolTipIsVisible, setToolTipIsVisible] = (0, _react.useState)(false);
  const [mouseLeaveTimer, setMouseLeaveTimer] = (0, _react.useState)(undefined);
  const handleKeyPress = (0, _react.useCallback)(event => {
    toggleStateOnKeyPress(toolTipIsVisible, setToolTipIsVisible)(event);
  }, [toolTipIsVisible]);
  const handleContentMouseOver = (0, _react.useCallback)(() => {
    mouseLeaveTimer && /* istanbul ignore next */clearTimeout(mouseLeaveTimer);
  }, [mouseLeaveTimer]);
  const handleMouseOver = (0, _react.useCallback)(() => {
    mouseLeaveTimer && clearTimeout(mouseLeaveTimer);
    setToolTipIsVisible(true);
  }, [mouseLeaveTimer]);
  const handleMouseLeave = (0, _react.useCallback)(() => {
    setMouseLeaveTimer(setTimeout(() => setToolTipIsVisible(false), delayHide));
  }, [delayHide]);
  const content = (0, _react.useMemo)(() => {
    return isComponent ? /*#__PURE__*/_react.default.createElement(TooltipContent, null) : /*#__PURE__*/_react.default.createElement("p", {
      className: (0, _classnames.default)([_style.default.tooltipContent, FontSizes[fontSize]])
    }, TooltipContent);
  }, [TooltipContent, fontSize, isComponent]);
  const anchorElement = (0, _react.useMemo)(() => AnchorElement ? /*#__PURE__*/_react.default.createElement(AnchorElement, {
    onKeyDown: handleKeyPress
  }) : /*#__PURE__*/_react.default.createElement("button", {
    type: "button",
    className: (0, _classnames.default)([_style.default.tooltipIconContainer, iconContainerClassName]),
    "data-testid": dataTestId,
    onKeyDown: handleKeyPress,
    tabIndex: 0
  }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyInformationIcon, {
    className: _style.default.informationIcon,
    width: IconSizes[iconSize],
    height: IconSizes[iconSize],
    "aria-label": ariaLabel
  })), [AnchorElement, ariaLabel, dataTestId, handleKeyPress, iconContainerClassName, iconSize]);
  return anchorId ? /*#__PURE__*/_react.default.createElement(ToolTipWrapper, {
    toolTipIsVisible: _toolTipIsVisible,
    anchorId: anchorId,
    closeToolTipInformationTextAriaLabel: closeToolTipInformationTextAriaLabel,
    content: content,
    onMouseLeave: handleMouseLeave,
    onMouseOver: handleMouseOver,
    fontSize: fontSize,
    placement: placement
  }) : /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.tooltipContainer,
    onMouseLeave: handleMouseLeave,
    onMouseOver: handleMouseOver
  }, anchorElement, /*#__PURE__*/_react.default.createElement(ToolTipWrapper, {
    toolTipIsVisible: toolTipIsVisible,
    anchorId: anchorId,
    closeToolTipInformationTextAriaLabel: closeToolTipInformationTextAriaLabel,
    content: content,
    handleContentMouseOver: handleContentMouseOver,
    fontSize: fontSize,
    iconSize: iconSize,
    tooltipClassName: tooltipClassName,
    usesAnchorElement: !(0, _isNil.default)(AnchorElement)
  }));
};
ToolTip.propTypes = process.env.NODE_ENV !== "production" ? {
  TooltipContent: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.node, _propTypes.default.string]),
  'data-testid': _propTypes.default.string,
  'aria-label': _propTypes.default.string,
  closeToolTipInformationTextAriaLabel: _propTypes.default.string.isRequired,
  // ---------- Regular Tooltip exclusive --------------
  // If using an Anchor element w/ the regular Tooltip
  AnchorElement: _propTypes.default.func,
  tooltipClassName: _propTypes.default.string,
  //
  iconContainerClassName: _propTypes.default.string,
  delayHide: _propTypes.default.number,
  fontSize: _propTypes.default.oneOf([12, 14]),
  iconSize: _propTypes.default.oneOf((0, _keys.default)(IconSizes)),
  // ----------  React Tooltip exclusive  --------------
  // externalHandling: if passed down, React Tooltip is used instead, due to limitations on
  // parents overflow hidden controls
  // data-for={anchorId} && data-tooltip-place="left" are needed on the anchored component
  anchorId: _propTypes.default.string,
  toolTipIsVisible: _propTypes.default.bool,
  placement: _propTypes.default.oneOf(['left', 'right', 'top', 'bottom'])
} : {};
var _default = exports.default = ToolTip;
//# sourceMappingURL=index.js.map