"use strict";

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

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

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

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

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

var _autoId = require("@reach/auto-id");

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

var _Box = require("../Box");

var _useDisclosure2 = require("../hooks/useDisclosure");

var _Popper = require("../Popper");

var _Text = require("../Text");

var _VisuallyHidden = require("../VisuallyHidden");

var _styles = _interopRequireDefault(require("./styles"));

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 __jsx = _react["default"].createElement;

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var wrapEvent = function wrapEvent(child, theirHandler, ourHandler) {
  return function (event) {
    if (typeof child !== 'string' && child.props[theirHandler]) {
      child.props[theirHandler](event);
    }

    if (!event.defaultPrevented) {
      return ourHandler(event);
    }

    return null;
  };
};

var Tooltip = function Tooltip(_ref) {
  var label = _ref.label,
      ariaLabel = _ref['aria-label'],
      _ref$showDelay = _ref.showDelay,
      showDelay = _ref$showDelay === void 0 ? 0 : _ref$showDelay,
      _ref$hideDelay = _ref.hideDelay,
      hideDelay = _ref$hideDelay === void 0 ? 0 : _ref$hideDelay,
      placement = _ref.placement,
      children = _ref.children,
      hasArrow = _ref.hasArrow,
      closeOnClick = _ref.closeOnClick,
      defaultIsOpen = _ref.defaultIsOpen,
      shouldWrapChildren = _ref.shouldWrapChildren,
      controlledIsOpen = _ref.isOpen,
      onOpenProp = _ref.onOpen,
      onCloseProp = _ref.onClose,
      _ref$showTooltip = _ref.showTooltip,
      showTooltip = _ref$showTooltip === void 0 ? true : _ref$showTooltip,
      rest = (0, _objectWithoutProperties2["default"])(_ref, ["label", "aria-label", "showDelay", "hideDelay", "placement", "children", "hasArrow", "closeOnClick", "defaultIsOpen", "shouldWrapChildren", "isOpen", "onOpen", "onClose", "showTooltip"]);

  var _useDisclosure = (0, _useDisclosure2.useDisclosure)(defaultIsOpen || false),
      isOpen = _useDisclosure.isOpen,
      onClose = _useDisclosure.onClose,
      onOpen = _useDisclosure.onOpen;

  var _useRef = (0, _react.useRef)(controlledIsOpen != null),
      isControlled = _useRef.current;

  var _isOpen = isControlled ? controlledIsOpen : isOpen;

  var referenceRef = (0, _react.useRef)();
  var enterTimeoutRef = (0, _react.useRef)();
  var exitTimeoutRef = (0, _react.useRef)();

  var openWithDelay = function openWithDelay() {
    enterTimeoutRef.current = setTimeout(onOpen, showDelay);
  };

  var closeWithDelay = function closeWithDelay() {
    exitTimeoutRef.current = setTimeout(onClose, hideDelay);
  };

  var tooltipId = "tooltip-".concat((0, _autoId.useId)());

  var handleOpen = function handleOpen() {
    if (!isControlled) {
      openWithDelay();
    }

    if (onOpenProp) {
      onOpenProp();
    }
  };

  var handleClose = function handleClose() {
    if (!isControlled) {
      closeWithDelay();
    }

    if (onCloseProp) {
      onCloseProp();
    }
  };

  var tooltipStyleProps = (0, _styles["default"])({
    placement: placement
  });
  var handleClick = wrapEvent(children, 'onClick', function () {
    if (closeOnClick) {
      closeWithDelay();
    }
  });

  var referenceProps = _objectSpread({
    onMouseEnter: wrapEvent(children, 'onMouseEnter', handleOpen),
    onMouseLeave: wrapEvent(children, 'onMouseLeave', handleClose),
    onClick: handleClick,
    onFocus: wrapEvent(children, 'onFocus', handleOpen),
    onBlur: wrapEvent(children, 'onBlur', handleClose)
  }, _isOpen && {
    'aria-describedby': tooltipId
  });

  var clone;

  if (typeof children === 'string' || shouldWrapChildren) {
    clone = __jsx(_Box.Box, (0, _extends2["default"])({
      as: "span",
      tabIndex: 0,
      ref: referenceRef
    }, referenceProps), children);
  } else {
    clone = __jsx(_Box.Box, {
      ref: referenceRef
    }, /*#__PURE__*/(0, _react.cloneElement)(_react.Children.only(children), referenceProps));
  }

  var hasAriaLabel = ariaLabel != null;
  return __jsx(_react["default"].Fragment, null, clone, showTooltip && __jsx(_Popper.Popper, (0, _extends2["default"])({
    usePortal: true,
    isOpen: _isOpen,
    modifiers: {
      offset: {
        enabled: true,
        offset: "0, 8"
      }
    },
    anchorEl: referenceRef.current,
    hasArrow: hasArrow,
    id: hasAriaLabel ? undefined : tooltipId,
    role: hasAriaLabel ? undefined : 'tooltip'
  }, tooltipStyleProps, rest), label && (typeof label === 'string' ? __jsx(_Text.Text, null, label) : __jsx(_Box.Box, null, label)), hasAriaLabel && __jsx(_VisuallyHidden.VisuallyHidden, {
    role: "tooltip",
    id: tooltipId
  }, ariaLabel), hasArrow && __jsx(_Popper.PopperArrow, null)));
};

exports.Tooltip = Tooltip;
//# sourceMappingURL=Tooltip.js.map