import { clsx } from 'clsx';
import { forwardRef, useState, useEffect, useContext } from 'react';
import { usePopper } from 'react-popper';
import '../theme.mjs';
import '../direction.mjs';
import '../propsValues/control.mjs';
import '../propsValues/breakpoint.mjs';
import '../propsValues/size.mjs';
import '../propsValues/typography.mjs';
import '../propsValues/width.mjs';
import '../propsValues/type.mjs';
import '../propsValues/dateMode.mjs';
import '../propsValues/monthFormat.mjs';
import { Position } from '../propsValues/position.mjs';
import '../propsValues/layouts.mjs';
import '../propsValues/status.mjs';
import '../propsValues/sentiment.mjs';
import '../propsValues/profileType.mjs';
import '../propsValues/variant.mjs';
import '../propsValues/scroll.mjs';
import '../propsValues/markdownNodeType.mjs';
import '../fileType.mjs';
import '@transferwise/icons';
import 'react-intl';
import '../closeButton/CloseButton.messages.mjs';
import { jsx, jsxs } from 'react/jsx-runtime';
import Dimmer from '../../dimmer/Dimmer.mjs';
import { OverlayIdContext } from '../../provider/overlay/OverlayIdProvider.mjs';

const POPOVER_OFFSET = [0, 16];
// By default the flip positioning explores only the opposite alternative. So if left is passed and there's no enough space
// the right one gets chosen. If there's no space on both sides popover goes back to the initially chosen one left.
// This mapping forces popover to try the four available positions before going back to the initial chosen one.
const fallbackPlacements = {
  [Position.TOP]: [Position.BOTTOM, Position.RIGHT, Position.LEFT],
  [Position.BOTTOM]: [Position.TOP, Position.RIGHT, Position.LEFT],
  [Position.LEFT]: [Position.RIGHT, Position.TOP, Position.BOTTOM],
  [Position.RIGHT]: [Position.LEFT, Position.TOP, Position.BOTTOM]
};
const Panel = /*#__PURE__*/forwardRef(function Panel({
  arrow = false,
  flip = true,
  altAxis = false,
  children,
  open = false,
  onClose,
  position = Position.BOTTOM,
  anchorRef,
  anchorWidth = false,
  considerHeight = false,
  ...rest
}, reference) {
  const [arrowElement, setArrowElement] = useState(null);
  const [popperElement, setPopperElement] = useState(null);
  const modifiers = [];
  if (altAxis) {
    modifiers.push({
      // https://popper.js.org/docs/v2/modifiers/prevent-overflow
      name: 'preventOverflow',
      options: {
        altAxis: true,
        tether: false
      }
    });
  }
  if (arrow) {
    modifiers.push({
      name: 'arrow',
      options: {
        element: arrowElement,
        options: {
          padding: 8 // 8px from the edges of the popper
        }
      }
    });
    // This lets you displace a popper element from its reference element.
    modifiers.push({
      name: 'offset',
      options: {
        offset: POPOVER_OFFSET
      }
    });
  }
  if (flip && fallbackPlacements[position]) {
    modifiers.push({
      name: 'flip',
      options: {
        fallbackPlacements: fallbackPlacements[position]
      }
    });
  }
  const {
    styles,
    attributes,
    forceUpdate
  } = usePopper(anchorRef.current, popperElement, {
    placement: position,
    modifiers
  });
  // If the trigger is not visible when the position is calculated, it will be incorrect. Because this can happen repeatedly (on resize for example),
  // it is most simple just to always position before opening
  useEffect(() => {
    if (open && forceUpdate) {
      forceUpdate();
    }
  }, [open]);
  const contentStyle = {
    ...(anchorWidth ? {
      width: anchorRef.current?.clientWidth
    } : undefined)
  };
  const overlayId = useContext(OverlayIdContext);
  return /*#__PURE__*/jsx(Dimmer, {
    open: open,
    transparent: true,
    fadeContentOnEnter: true,
    fadeContentOnExit: true,
    onClose: onClose,
    children: /*#__PURE__*/jsx("div", {
      id: overlayId,
      ...rest,
      ref: setPopperElement,
      role: "dialog",
      style: {
        ...styles.popper
      },
      ...attributes.popper,
      className: clsx('np-panel', {
        'np-panel--open': open
      }, rest.className),
      children: /*#__PURE__*/jsxs("div", {
        ref: reference,
        style: contentStyle,
        className: clsx('np-panel__content'),
        children: [children, arrow && /*#__PURE__*/jsx("div", {
          ref: setArrowElement,
          className: clsx('np-panel__arrow'),
          style: styles.arrow
        })]
      })
    })
  });
});

export { Panel as default };
//# sourceMappingURL=Panel.mjs.map
