'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var react = require('@floating-ui/react');
var React = require('react');
var react$1 = require('../../utils/react.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

// TODO: #2003
var LIST_PADDING = 2;
var FloatingPopover = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
  var target = _ref.target,
    children = _ref.children,
    opened = _ref.opened,
    onToggle = _ref.onToggle,
    placement = _ref.placement,
    portal = _ref.portal,
    offset = _ref.offset,
    isInner = _ref.isInner,
    trigger = _ref.trigger,
    zIndex = _ref.zIndex;
  var _useFloating = react.useFloating({
      whileElementsMounted: function whileElementsMounted(referenceEl, floatingEl, update) {
        return react.autoUpdate(referenceEl, floatingEl, update, {
          ancestorScroll: false,
          ancestorResize: false,
          layoutShift: false
        });
      },
      placement: placement === 'auto' ? undefined : placement,
      open: opened,
      onOpenChange: onToggle,
      middleware: [placement === 'auto' && react.autoPlacement(), react.offset({
        mainAxis: isInner ? LIST_PADDING * 2 : offset === null || offset === void 0 ? void 0 : offset[1],
        crossAxis: isInner ? -LIST_PADDING : offset === null || offset === void 0 ? void 0 : offset[0]
      }), react.flip({
        fallbackAxisSideDirection: 'end'
      }), react.shift()]
    }),
    refs = _useFloating.refs,
    floatingStyles = _useFloating.floatingStyles,
    context = _useFloating.context;
  var wrappedId = react$1.safeUseId();
  var hover = react.useHover(context, {
    enabled: trigger === 'hover',
    delay: {
      open: 0,
      close: 50
    },
    handleClose: react.safePolygon({
      requireIntent: false
    })
  });
  var click = react.useClick(context, {
    stickIfOpen: false,
    ignoreMouse: trigger === 'hover'
  });
  var _useInteractions = react.useInteractions([hover, click]),
    getReferenceProps = _useInteractions.getReferenceProps;
  return /*#__PURE__*/React__default.default.createElement("div", {
    ref: ref,
    id: wrappedId,
    style: {
      position: 'relative',
      display: isInner ? 'block' : 'inline-block'
    }
  }, /*#__PURE__*/React__default.default.createElement("div", _rollupPluginBabelHelpers.extends({
    ref: refs.setReference
  }, getReferenceProps()), target), opened && /*#__PURE__*/React__default.default.createElement(react.FloatingPortal, getFloatingPortalProps(portal, wrappedId), /*#__PURE__*/React__default.default.createElement("div", {
    ref: refs.setFloating,
    style: _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, floatingStyles), {}, {
      zIndex: zIndex
    })
  }, children)));
});
// root - принимает ref контейнера портала.
// id - если есть портал - не используется, если портала нет - подставляется 'wrappedId'.
var getFloatingPortalProps = function getFloatingPortalProps(portal, wrappedId) {
  if (!portal) {
    return {
      id: wrappedId
    };
  }
  if (typeof portal === 'string') {
    return {
      id: portal
    };
  }
  return {
    root: portal
  };
};

exports.FloatingPopover = FloatingPopover;
