function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var _excluded = ["className", "id", "label", "ariaLabel", "position", "positionCallback", "inModal", "maxWidth", "triggerElement", "pointerEvents", "offset", "backgroundColor", "borderColor", "handleMouseEnter", "handleMouseLeave", "customBoundary", "pointerArrow", "bottomSheet", "bottomSheetTitle", "onBottomSheetClose", "invertedColors", "setPopoverElement"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { usePopper } from 'react-popper';
import { motion } from 'framer-motion';
import { mergeRefs } from '../utils';
import { Portal } from '../..';
import { TooltipArrow } from './TooltipArrow';
import { StyledSpan, StyledTooltipContent, bottomSheetStyles, hideArrowStyles } from './PopOver.styles';
import { useMouseEvents } from './hooks/useMouseEvents';
var displayName = 'Tooltip Popup';
var components = {
  TooltipContent: StyledTooltipContent
};
var PopOver = function PopOver(_ref) {
  var className = _ref.className,
    id = _ref.id,
    label = _ref.label,
    ariaLabel = _ref.ariaLabel,
    position = _ref.position,
    positionCallback = _ref.positionCallback,
    inModal = _ref.inModal,
    maxWidth = _ref.maxWidth,
    triggerElement = _ref.triggerElement,
    _ref$pointerEvents = _ref.pointerEvents,
    pointerEvents = _ref$pointerEvents === void 0 ? true : _ref$pointerEvents,
    offset = _ref.offset,
    backgroundColorProp = _ref.backgroundColor,
    borderColorProp = _ref.borderColor,
    handleMouseEnter = _ref.handleMouseEnter,
    handleMouseLeave = _ref.handleMouseLeave,
    customBoundary = _ref.customBoundary,
    _ref$pointerArrow = _ref.pointerArrow,
    pointerArrow = _ref$pointerArrow === void 0 ? true : _ref$pointerArrow,
    _ref$bottomSheet = _ref.bottomSheet,
    bottomSheet = _ref$bottomSheet === void 0 ? false : _ref$bottomSheet,
    bottomSheetTitle = _ref.bottomSheetTitle,
    _ref$onBottomSheetClo = _ref.onBottomSheetClose,
    onBottomSheetClose = _ref$onBottomSheetClo === void 0 ? function () {} : _ref$onBottomSheetClo,
    invertedColors = _ref.invertedColors,
    setPopoverElement = _ref.setPopoverElement,
    htmlSpanProps = _objectWithoutProperties(_ref, _excluded);
  var _useState = useState(null),
    _useState2 = _slicedToArray(_useState, 2),
    popperElement = _useState2[0],
    setPopperElement = _useState2[1];
  var _useState3 = useState(null),
    _useState4 = _slicedToArray(_useState3, 2),
    arrowElement = _useState4[0],
    setArrowElement = _useState4[1];
  var overrideStyles = useCallback(function (args) {
    var state = args.state;
    var popperStyles = bottomSheet ? bottomSheetStyles : state.styles.popper;
    return _objectSpread(_objectSpread({}, state), {}, {
      styles: _objectSpread(_objectSpread({}, state.styles), {}, {
        popper: popperStyles,
        arrow: bottomSheet ? hideArrowStyles : state.styles.arrow
      })
    });
  }, [bottomSheet]);
  var flipMods = customBoundary ? [{
    name: 'preventOverflow',
    options: {
      boundary: customBoundary
    }
  }, {
    name: 'flip',
    options: {
      boundary: customBoundary
    }
  }] : [];
  var modifiers = [{
    name: 'overrideMobileStyles',
    enabled: bottomSheet,
    phase: 'write',
    fn: overrideStyles
  }, {
    name: 'offset',
    options: {
      offset: offset
    },
    enabled: !!offset
  }].concat(flipMods, [{
    name: 'arrow',
    options: {
      element: arrowElement
    }
  }]);
  var ref = useRef();
  useEffect(function () {
    if (setPopoverElement && ref && bottomSheet && pointerEvents) {
      setPopoverElement(ref);
    }
  }, [setPopoverElement, ref, bottomSheet, pointerEvents]);
  var popper = usePopper(triggerElement, popperElement, {
    modifiers: modifiers,
    placement: position
  });
  var state = popper.state,
    styles = popper.styles,
    attributes = popper.attributes;
  var _ref2 = state || {},
    placement = _ref2.placement;
  if (positionCallback && placement) {
    positionCallback(placement);
  }
  var backgroundColor = backgroundColorProp || function (t) {
    return invertedColors ? t.color.bubbleBackgroundInverted : t.color.bubbleBackground;
  };
  var borderColor = borderColorProp || function (t) {
    return invertedColors ? t.color.bubbleBorderInverted : t.color.bubbleBorder;
  };
  useMouseEvents(ref, pointerEvents, handleMouseEnter, handleMouseLeave);
  var content = React.createElement(StyledSpan, _extends({
    className: className,
    id: id,
    ref: mergeRefs([setPopperElement, ref]),
    $inModal: inModal || bottomSheet,
    style: styles.popper,
    $pointerEvents: pointerEvents
  }, htmlSpanProps, attributes.popper), React.createElement(motion.div, {
    key: "bottomsheet",
    initial: bottomSheet ? {
      y: 100,
      opacity: 0
    } : {
      opacity: 0
    },
    exit: bottomSheet ? {
      y: 100,
      opacity: 0
    } : {
      opacity: 0
    },
    animate: {
      y: 0,
      opacity: 1,
      transition: {
        duration: 0.2
      }
    },
    transition: {
      type: 'ease',
      duration: 0.2
    }
  }, pointerArrow && React.createElement(TooltipArrow, {
    ref: setArrowElement,
    position: state === null || state === void 0 ? void 0 : state.placement,
    style: styles.arrow,
    backgroundColor: backgroundColor,
    borderColor: borderColor
  }), React.createElement(StyledTooltipContent, {
    label: label,
    bottomSheet: bottomSheet,
    bottomSheetTitle: bottomSheetTitle,
    onBottomSheetClose: onBottomSheetClose,
    ariaLabel: ariaLabel,
    maxWidth: maxWidth,
    backgroundColor: backgroundColor,
    borderColor: borderColor,
    invertedColors: invertedColors
  })));
  if (!customBoundary) {
    return React.createElement(Portal, null, content);
  }
  return content;
};
PopOver.components = components;
PopOver.displayName = displayName;
export default PopOver;