"use strict";

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

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

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

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

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

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

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

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

var _lodash = _interopRequireDefault(require("lodash.throttle"));

var _overlay = _interopRequireDefault(require("../overlay"));

var _Transition = _interopRequireDefault(require("react-transition-group/Transition"));

var _reactResizeDetector = require("react-resize-detector");

var _utils = require("../utils");

var _positionCalculation = require("./positionCalculation");

var _sizeCalculation = require("./sizeCalculation");

var _box = _interopRequireDefault(require("../box"));

var _theme = _interopRequireDefault(require("./theme.css"));

var _uiUtilities = _interopRequireDefault(require("@teamleader/ui-utilities"));

var _useFocusTrap2 = _interopRequireDefault(require("../../utils/useFocusTrap"));

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 Popover = function Popover(props) {
  var _useState = (0, _react.useState)({
    left: 0,
    top: 0
  }),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      positioning = _useState2[0],
      setPositioning = _useState2[1];

  var _props$active = props.active,
      active = _props$active === void 0 ? true : _props$active,
      _props$backdrop = props.backdrop,
      backdrop = _props$backdrop === void 0 ? 'dark' : _props$backdrop,
      children = props.children,
      className = props.className,
      _props$color = props.color,
      color = _props$color === void 0 ? 'neutral' : _props$color,
      _props$fullHeight = props.fullHeight,
      fullHeight = _props$fullHeight === void 0 ? true : _props$fullHeight,
      _props$fullWidth = props.fullWidth,
      fullWidth = _props$fullWidth === void 0 ? false : _props$fullWidth,
      _props$lockScroll = props.lockScroll,
      lockScroll = _props$lockScroll === void 0 ? true : _props$lockScroll,
      _props$maxWidth = props.maxWidth,
      maxWidth = _props$maxWidth === void 0 ? '50vw' : _props$maxWidth,
      _props$minWidth = props.minWidth,
      minWidth = _props$minWidth === void 0 ? '180px' : _props$minWidth,
      onOverlayClick = props.onOverlayClick,
      onEscKeyDown = props.onEscKeyDown,
      _props$tint = props.tint,
      tint = _props$tint === void 0 ? 'lightest' : _props$tint,
      _props$zIndex = props.zIndex,
      zIndex = _props$zIndex === void 0 ? 300 : _props$zIndex,
      anchorEl = props.anchorEl,
      _props$direction = props.direction,
      direction = _props$direction === void 0 ? 'south' : _props$direction,
      _props$position = props.position,
      position = _props$position === void 0 ? 'center' : _props$position,
      _props$offsetCorrecti = props.offsetCorrection,
      offsetCorrection = _props$offsetCorrecti === void 0 ? 0 : _props$offsetCorrecti,
      _props$returnFocusToS = props.returnFocusToSource,
      returnFocusToSource = _props$returnFocusToS === void 0 ? true : _props$returnFocusToS;

  var _useFocusTrap = (0, _useFocusTrap2.default)({
    active: active,
    returnFocusToSource: returnFocusToSource,
    initialFocusRef: undefined
  }),
      ref = _useFocusTrap.ref,
      FocusRing = _useFocusTrap.FocusRing;

  var handleResize = function handleResize() {
    if (ref.current && anchorEl) {
      setPositioning((0, _positionCalculation.calculatePositions)(anchorEl, ref.current, direction, position, offsetCorrection));
    }
  };

  (0, _reactResizeDetector.useResizeDetector)({
    handleHeight: true,
    handleWidth: true,
    onResize: handleResize,
    refreshMode: 'throttle',
    refreshRate: 250
  });
  (0, _react.useEffect)(function () {
    var handleResizeThrottled = (0, _lodash.default)(handleResize, 250);

    _utils.events.addEventsToWindow({
      resize: handleResizeThrottled,
      scroll: handleResizeThrottled
    });

    return function cleanup() {
      _utils.events.removeEventsFromWindow({
        resize: handleResizeThrottled,
        scroll: handleResizeThrottled
      });
    };
  }, [handleResize]);
  (0, _react.useEffect)(function () {
    if (active) {
      handleResize();
    }
  }, [props]);
  var left = positioning.left,
      top = positioning.top,
      maxHeight = positioning.maxHeight;

  if (!active) {
    return null;
  }

  var popover = /*#__PURE__*/_react.default.createElement(_Transition.default, {
    timeout: 0,
    in: active,
    appear: true
  }, function (state) {
    var _cx;

    return /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _classnames.default)(_theme.default['wrapper'], _theme.default[color], _theme.default[tint], (_cx = {}, (0, _defineProperty2.default)(_cx, _theme.default['is-entering'], state === 'entering'), (0, _defineProperty2.default)(_cx, _theme.default['is-entered'], state === 'entered'), _cx)),
      style: {
        zIndex: zIndex
      }
    }, /*#__PURE__*/_react.default.createElement(_overlay.default, {
      active: active,
      backdrop: backdrop,
      className: _theme.default['overlay'],
      lockScroll: lockScroll,
      onOverlayClick: onOverlayClick,
      onEscKeyDown: onEscKeyDown
    }, /*#__PURE__*/_react.default.createElement(FocusRing, null, /*#__PURE__*/_react.default.createElement(_box.default, {
      "data-teamleader-ui": "popover",
      className: (0, _classnames.default)(_uiUtilities.default['box-shadow-200'], _theme.default['popover'], className),
      style: {
        left: "".concat(left, "px"),
        top: "".concat(top, "px"),
        maxWidth: fullWidth ? '100vw' : maxWidth,
        minWidth: minWidth
      },
      ref: ref
    }, /*#__PURE__*/_react.default.createElement(_box.default, {
      className: _theme.default['inner'],
      display: "flex",
      flex: "1 1 auto",
      flexDirection: "column",
      style: {
        maxHeight: maxHeight && (0, _sizeCalculation.getMaxHeight)(fullHeight, maxHeight)
      }
    }, children)))));
  });

  return /*#__PURE__*/(0, _reactDom.createPortal)(popover, document.body);
};

var _default = Popover;
exports.default = _default;