import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["className", "dark", "noPadding", "children", "content", "trigger", "modifiers", "placement", "createRefWrapper", "closeOnOutsideClick", "closeOnTriggerHidden", "defaultVisible", "delayHide", "delayShow", "followCursor", "interactive", "mutationObserverOptions", "offset", "onVisibleChange", "usePortal"];
import React, { useState } from 'react';
import ReactDOM from 'react-dom';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import { usePopperTooltip } from 'react-popper-tooltip';
import { settings } from '../../globals/js';
var prefix = settings.prefix;
export var tooltipStyle = {
  duration: 100,
  animation: 'fade',
  theme: 'light',
  arrow: true
};
export 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. */

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 = _objectWithoutProperties(_ref, _excluded);

  var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      visibility = _useState2[0],
      setVisibility = _useState2[1];

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

  var _usePopperTooltip = 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 = classnames(children === null || children === void 0 ? void 0 : (_children$props = children.props) === null || _children$props === void 0 ? void 0 : _children$props.className, _defineProperty({}, "".concat(prefix, "--tooltip--trigger"), true));
  var wrapperClassNames = classnames(className, _defineProperty({}, "".concat(prefix, "--tooltip--trigger"), true));
  var triggerElement = !createRefWrapper && typeof children !== 'string' ? /*#__PURE__*/React.cloneElement(children, {
    ref: setTriggerRef,
    className: elementClassNames
  }) : /*#__PURE__*/React.createElement("span", {
    ref: setTriggerRef,
    className: wrapperClassNames
  }, children);

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

  var tooltip = /*#__PURE__*/React.createElement("div", _extends({
    ref: setTooltipRef
  }, getTooltipProps({
    className: classNames
  })), typeof content === 'function' ? content({
    setVisibility: setVisibility,
    visibilityChange: visibilityChange
  }) : content, /*#__PURE__*/React.createElement("div", getArrowProps({
    className: "".concat(prefix, "--tooltip__arrow")
  })));
  return /*#__PURE__*/React.createElement(React.Fragment, null, triggerElement, visible && usePortal && /*#__PURE__*/React.createElement(TooltipPortal, null, tooltip), visible && !usePortal && tooltip);
};

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

  /**
   * Provide the content for the tooltip
   */
  content: PropTypes.node,

  /**
   * Provide a dark styled tooltip
   */
  dark: PropTypes.bool,

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

  /**
   * Provide the placement of the tooltip
   */
  placement: PropTypes.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.oneOf(['hover', 'click']),

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

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

  /**
   * Use a wrapper html element around the trigger. Useful for components without `forwardRef` support.
   */
  createRefWrapper: PropTypes.bool
};
export default Tooltip;

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