"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.tooltipStyleDark = exports.tooltipStyle = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _react = _interopRequireWildcard(require("react"));

var _reactDom = _interopRequireDefault(require("react-dom"));

var _classnames4 = _interopRequireDefault(require("classnames"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _reactPopperTooltip = require("react-popper-tooltip");

var _js = require("../../globals/js");

var _excluded = ["className", "dark", "noPadding", "children", "content", "trigger", "modifiers", "placement", "createRefWrapper", "closeOnOutsideClick", "closeOnTriggerHidden", "defaultVisible", "delayHide", "delayShow", "followCursor", "interactive", "mutationObserverOptions", "offset", "onVisibleChange", "usePortal"];

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var prefix = _js.settings.prefix;
var tooltipStyle = {
  duration: 100,
  animation: 'fade',
  theme: 'light',
  arrow: true
};
exports.tooltipStyle = tooltipStyle;
var tooltipStyleDark = {
  duration: 100,
  animation: 'fade',
  theme: 'dark',
  arrow: true
};
/** Tooltips display additional information upon click, hover, or focus. The information should be contextual, useful, and nonessential. */

exports.tooltipStyleDark = tooltipStyleDark;

var Tooltip = function Tooltip(_ref) {
  var _classnames, _children$props;

  var className = _ref.className,
      dark = _ref.dark,
      noPadding = _ref.noPadding,
      children = _ref.children,
      content = _ref.content,
      _ref$trigger = _ref.trigger,
      trigger = _ref$trigger === void 0 ? 'hover' : _ref$trigger,
      _ref$modifiers = _ref.modifiers,
      modifiers = _ref$modifiers === void 0 ? [] : _ref$modifiers,
      _ref$placement = _ref.placement,
      placement = _ref$placement === void 0 ? 'top' : _ref$placement,
      createRefWrapper = _ref.createRefWrapper,
      closeOnOutsideClick = _ref.closeOnOutsideClick,
      closeOnTriggerHidden = _ref.closeOnTriggerHidden,
      defaultVisible = _ref.defaultVisible,
      delayHide = _ref.delayHide,
      delayShow = _ref.delayShow,
      followCursor = _ref.followCursor,
      interactive = _ref.interactive,
      mutationObserverOptions = _ref.mutationObserverOptions,
      offset = _ref.offset,
      onVisibleChange = _ref.onVisibleChange,
      usePortal = _ref.usePortal,
      others = (0, _objectWithoutProperties2.default)(_ref, _excluded);

  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      visibility = _useState2[0],
      setVisibility = _useState2[1];

  var classNames = (0, _classnames4.default)(className, (_classnames = {}, (0, _defineProperty2.default)(_classnames, "".concat(prefix, "--tooltip"), true), (0, _defineProperty2.default)(_classnames, "".concat(prefix, "--tooltip--disable-padding"), noPadding), (0, _defineProperty2.default)(_classnames, "".concat(prefix, "--tooltip--visible"), visibility), (0, _defineProperty2.default)(_classnames, "".concat(prefix, "--tooltip--").concat(trigger), trigger), (0, _defineProperty2.default)(_classnames, "".concat(prefix, "--tooltip--dark"), dark), _classnames));

  var _usePopperTooltip = (0, _reactPopperTooltip.usePopperTooltip)({
    closeOnOutsideClick: closeOnOutsideClick,
    closeOnTriggerHidden: closeOnTriggerHidden,
    defaultVisible: defaultVisible,
    delayHide: delayHide,
    delayShow: delayShow,
    followCursor: followCursor,
    interactive: interactive,
    mutationObserverOptions: mutationObserverOptions,
    offset: offset,
    onVisibleChange: onVisibleChange,
    placement: placement,
    trigger: trigger
  }),
      getArrowProps = _usePopperTooltip.getArrowProps,
      getTooltipProps = _usePopperTooltip.getTooltipProps,
      setTooltipRef = _usePopperTooltip.setTooltipRef,
      setTriggerRef = _usePopperTooltip.setTriggerRef,
      visible = _usePopperTooltip.visible;

  var elementClassNames = (0, _classnames4.default)(children === null || children === void 0 ? void 0 : (_children$props = children.props) === null || _children$props === void 0 ? void 0 : _children$props.className, (0, _defineProperty2.default)({}, "".concat(prefix, "--tooltip--trigger"), true));
  var wrapperClassNames = (0, _classnames4.default)(className, (0, _defineProperty2.default)({}, "".concat(prefix, "--tooltip--trigger"), true));
  var triggerElement = !createRefWrapper && typeof children !== 'string' ? /*#__PURE__*/_react.default.cloneElement(children, {
    ref: setTriggerRef,
    className: elementClassNames
  }) : /*#__PURE__*/_react.default.createElement("span", {
    ref: setTriggerRef,
    className: wrapperClassNames
  }, children);

  var visibilityChange = function visibilityChange(e) {
    setVisibility(e);
    if (onVisibilityChange) onVisibilityChange(e);
  };

  var tooltip = /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
    ref: setTooltipRef
  }, getTooltipProps({
    className: classNames
  })), typeof content === 'function' ? content({
    setVisibility: setVisibility,
    visibilityChange: visibilityChange
  }) : content, /*#__PURE__*/_react.default.createElement("div", getArrowProps({
    className: "".concat(prefix, "--tooltip__arrow")
  })));

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, triggerElement, visible && usePortal && /*#__PURE__*/_react.default.createElement(TooltipPortal, null, tooltip), visible && !usePortal && tooltip);
};

Tooltip.propTypes = {
  /**
   * Provide the children on which the tooltip will show on
   */
  children: _propTypes.default.node,

  /**
   * Provide the content for the tooltip
   */
  content: _propTypes.default.node,

  /**
   * Provide a dark styled tooltip
   */
  dark: _propTypes.default.bool,

  /**
   * Disable the default inner padding of the tooltip
   */
  noPadding: _propTypes.default.bool,

  /**
   * Provide the placement of the tooltip
   */
  placement: _propTypes.default.oneOf(['top', 'top-start', 'top-end', 'right', 'right-start', 'right-end', 'bottom', 'bottom-start', 'bottom-end', 'left', 'left-start', 'left-end']),

  /**
   * Provide the placement of the tooltip
   */
  trigger: _propTypes.default.oneOf(['hover', 'click']),

  /**
   * Provide additional modifiers as an object https://popper.js.org/docs/v2/modifiers/
   */
  modifiers: _propTypes.default.array,

  /**
   * Whether to use React.createPortal for creating tooltip.
   */
  usePortal: _propTypes.default.bool,

  /**
   * Use a wrapper html element around the trigger. Useful for components without `forwardRef` support.
   */
  createRefWrapper: _propTypes.default.bool
};
var _default = Tooltip;
exports.default = _default;

function TooltipPortal(_ref2) {
  var children = _ref2.children;
  return /*#__PURE__*/_reactDom.default.createPortal(children, document.body);
}