'use strict';

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

var react = require('@floating-ui/react');
var clsx = require('clsx');
var React = require('react');
require('../theme.js');
require('../direction.js');
require('@transferwise/neptune-tokens');
require('../propsValues/control.js');
require('../propsValues/size.js');
require('../propsValues/typography.js');
require('../propsValues/width.js');
require('../propsValues/type.js');
require('../propsValues/dateMode.js');
require('../propsValues/monthFormat.js');
var position = require('../propsValues/position.js');
require('../propsValues/layouts.js');
require('../propsValues/status.js');
require('../propsValues/sentiment.js');
require('../propsValues/profileType.js');
require('../propsValues/variant.js');
require('../propsValues/scroll.js');
require('../propsValues/markdownNodeType.js');
require('../fileType.js');
require('@transferwise/formatting');
require('@transferwise/icons');
require('react-intl');
require('../closeButton/CloseButton.messages.js');
var jsxRuntime = require('react/jsx-runtime');
var Dimmer = require('../../dimmer/Dimmer.js');
var OverlayIdProvider = require('../../provider/overlay/OverlayIdProvider.js');

const POPOVER_OFFSET = 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.Position.TOP]: [position.Position.BOTTOM, position.Position.RIGHT, position.Position.LEFT],
  [position.Position.BOTTOM]: [position.Position.TOP, position.Position.RIGHT, position.Position.LEFT],
  [position.Position.LEFT]: [position.Position.RIGHT, position.Position.TOP, position.Position.BOTTOM],
  [position.Position.RIGHT]: [position.Position.LEFT, position.Position.TOP, position.Position.BOTTOM]
};
const Panel = /*#__PURE__*/React.forwardRef(function Panel({
  arrow = false,
  flip = true,
  altAxis = false,
  children,
  open = false,
  onClose,
  position: position$1 = position.Position.BOTTOM,
  anchorRef,
  anchorWidth = false,
  considerHeight = false,
  ...rest
}, reference) {
  const arrowRef = React.useRef(null);
  const middleware = [];
  if (arrow) {
    middleware.push(react.offset(POPOVER_OFFSET));
  }
  if (flip && fallbackPlacements[position$1]) {
    middleware.push(react.flip({
      fallbackPlacements: fallbackPlacements[position$1]
    }));
  }
  if (altAxis) {
    middleware.push(react.shift());
  }
  if (arrow) {
    middleware.push(react.arrow({
      element: arrowRef,
      padding: 8
    }));
  }
  if (considerHeight) {
    middleware.push(react.size({
      padding: POPOVER_OFFSET,
      apply: ({
        elements,
        availableHeight
      }) => {
        elements.floating.style.setProperty('--np-panel-available-height', `${availableHeight}px`);
      }
    }));
  }
  const {
    refs,
    floatingStyles,
    placement,
    middlewareData
  } = react.useFloating({
    placement: position$1,
    middleware,
    elements: {
      reference: anchorRef.current
    },
    whileElementsMounted: open ? react.autoUpdate : undefined
  });
  const arrowStyle = {
    ...(middlewareData.arrow?.x != null ? {
      left: middlewareData.arrow.x
    } : {}),
    ...(middlewareData.arrow?.y != null ? {
      top: middlewareData.arrow.y
    } : {})
  };
  const contentStyle = {
    ...(anchorWidth ? {
      width: anchorRef.current?.clientWidth
    } : undefined)
  };
  const overlayId = React.useContext(OverlayIdProvider.OverlayIdContext);
  return /*#__PURE__*/jsxRuntime.jsx(Dimmer.default, {
    open: open,
    transparent: true,
    fadeContentOnEnter: true,
    fadeContentOnExit: true,
    onClose: onClose,
    children: /*#__PURE__*/jsxRuntime.jsx("div", {
      id: overlayId,
      ...rest,
      ref: refs.setFloating,
      role: "dialog",
      style: floatingStyles,
      "data-popper-placement": placement,
      className: clsx.clsx('np-panel', {
        'np-panel--open': open
      }, rest.className),
      children: /*#__PURE__*/jsxRuntime.jsxs("div", {
        ref: reference,
        style: contentStyle,
        className: clsx.clsx('np-panel__content'),
        children: [children, arrow && /*#__PURE__*/jsxRuntime.jsx("div", {
          ref: arrowRef,
          className: clsx.clsx('np-panel__arrow'),
          style: arrowStyle
        })]
      })
    })
  });
});

exports.default = Panel;
//# sourceMappingURL=Panel.js.map
