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 _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; }
import React, { useState } from 'react';
import styled from 'styled-components';
import { usePopper } from 'react-popper';
import { Portal } from '../../../../..';
import { mergeRefs } from '../../../../../common/utils';
var getWidth = function getWidth(p) {
  var _p$triggerElement$cur;
  if (p.width) return p.width;
  if (p.noFormField) {
    return 'auto';
  }
  return "".concat((_p$triggerElement$cur = p.triggerElement.current) === null || _p$triggerElement$cur === void 0 ? void 0 : _p$triggerElement$cur.offsetWidth, "px");
};
var getStylesForPosition = function getStylesForPosition(listPosition) {
  switch (listPosition) {
    case 'left':
      return "right: -1px;";
    default:
      return "left: -1px;";
  }
};
var StyledListWrapper = styled.div.withConfig({
  displayName: "ListWithPortal__StyledListWrapper",
  componentId: "sc-1ye0klu-0"
})(["", " z-index:", ";padding:0 4px;width:", ";"], function (p) {
  return getStylesForPosition(p.listPosition);
}, function (p) {
  return p.theme.zIndex.overlayInModal;
}, getWidth);
export var ListWrapperWithPortal = React.forwardRef(function (_ref, ref) {
  var Component = _ref.component,
    triggerElement = _ref.triggerElement,
    children = _ref.children,
    noFormField = _ref.noFormField,
    onKeyDown = _ref.onKeyDown,
    onFocus = _ref.onFocus,
    onBlur = _ref.onBlur,
    onMouseMove = _ref.onMouseMove,
    searchComponent = _ref.searchComponent,
    actionsComponent = _ref.actionsComponent,
    width = _ref.width,
    dataTestId = _ref['data-testid'],
    maxHeight = _ref.maxHeight,
    listPosition = _ref.listPosition,
    placement = _ref.placement;
  var _useState = useState(null),
    _useState2 = _slicedToArray(_useState, 2),
    popperElement = _useState2[0],
    setPopperElement = _useState2[1];
  var _usePopper = usePopper(triggerElement.current, popperElement, {
      placement: placement || 'bottom'
    }),
    styles = _usePopper.styles,
    attributes = _usePopper.attributes,
    state = _usePopper.state;
  return React.createElement(Portal, null, React.createElement(StyledListWrapper, _extends({
    ref: mergeRefs([setPopperElement, ref]),
    noFormField: noFormField,
    onKeyDown: onKeyDown,
    onFocus: onFocus,
    onMouseMove: onMouseMove,
    onBlur: onBlur,
    triggerElement: triggerElement,
    width: width,
    listPosition: listPosition,
    style: styles.popper
  }, attributes.popper), React.createElement(Component, {
    searchComponent: searchComponent,
    actionsComponent: actionsComponent,
    maxHeight: maxHeight,
    listPosition: listPosition,
    noFormField: noFormField,
    placement: state === null || state === void 0 ? void 0 : state.placement
  }, children)));
});