import { extends as _extends, objectSpread2 as _objectSpread2 } from '../../_virtual/_rollupPluginBabelHelpers.js';
import { useFloating, autoUpdate, autoPlacement, offset, flip, shift, useHover, safePolygon, useClick, useInteractions, FloatingPortal } from '@floating-ui/react';
import React__default, { forwardRef } from 'react';
import { safeUseId } from '../../utils/react.js';

// TODO: #2003
var LIST_PADDING = 2;
var FloatingPopover = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var target = _ref.target,
    children = _ref.children,
    opened = _ref.opened,
    onToggle = _ref.onToggle,
    placement = _ref.placement,
    portal = _ref.portal,
    offset$1 = _ref.offset,
    isInner = _ref.isInner,
    trigger = _ref.trigger,
    zIndex = _ref.zIndex;
  var _useFloating = useFloating({
      whileElementsMounted: function whileElementsMounted(referenceEl, floatingEl, update) {
        return autoUpdate(referenceEl, floatingEl, update, {
          ancestorScroll: false,
          ancestorResize: false,
          layoutShift: false
        });
      },
      placement: placement === 'auto' ? undefined : placement,
      open: opened,
      onOpenChange: onToggle,
      middleware: [placement === 'auto' && autoPlacement(), offset({
        mainAxis: isInner ? LIST_PADDING * 2 : offset$1 === null || offset$1 === void 0 ? void 0 : offset$1[1],
        crossAxis: isInner ? -LIST_PADDING : offset$1 === null || offset$1 === void 0 ? void 0 : offset$1[0]
      }), flip({
        fallbackAxisSideDirection: 'end'
      }), shift()]
    }),
    refs = _useFloating.refs,
    floatingStyles = _useFloating.floatingStyles,
    context = _useFloating.context;
  var wrappedId = safeUseId();
  var hover = useHover(context, {
    enabled: trigger === 'hover',
    delay: {
      open: 0,
      close: 50
    },
    handleClose: safePolygon({
      requireIntent: false
    })
  });
  var click = useClick(context, {
    stickIfOpen: false,
    ignoreMouse: trigger === 'hover'
  });
  var _useInteractions = useInteractions([hover, click]),
    getReferenceProps = _useInteractions.getReferenceProps;
  return /*#__PURE__*/React__default.createElement("div", {
    ref: ref,
    id: wrappedId,
    style: {
      position: 'relative',
      display: isInner ? 'block' : 'inline-block'
    }
  }, /*#__PURE__*/React__default.createElement("div", _extends({
    ref: refs.setReference
  }, getReferenceProps()), target), opened && /*#__PURE__*/React__default.createElement(FloatingPortal, getFloatingPortalProps(portal, wrappedId), /*#__PURE__*/React__default.createElement("div", {
    ref: refs.setFloating,
    style: _objectSpread2(_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
  };
};

export { FloatingPopover };
