import { useFloating, offset, flip, shift, size, autoUpdate, useDismiss, useRole, useInteractions, FloatingPortal, FloatingFocusManager } from '@floating-ui/react';
import { Transition } from '@headlessui/react';
import { FocusScope } from '@react-aria/focus';
import { useTheme, ThemeProvider } from '@wise/components-theming';
import { clsx } from 'clsx';
import { useState } from 'react';
import { PreventScroll } from '../common/preventScroll/PreventScroll.mjs';
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';

const floatingPadding = 16;
function Popover({
  placement,
  open,
  renderTrigger,
  title,
  size: size$1 = 'md',
  padding = 'md',
  children,
  onClose,
  onCloseEnd
}) {
  const {
    refs,
    floatingStyles,
    context
  } = useFloating({
    placement,
    middleware: [offset(8), flip({
      padding: floatingPadding,
      crossAxis: false
    }), shift(), size({
      padding: floatingPadding,
      apply: ({
        elements,
        rects,
        availableHeight
      }) => {
        elements.floating.style.setProperty('--max-height', `${availableHeight}px`);
        elements.floating.style.setProperty('--width', `${rects.reference.width}px`);
      }
    })],
    whileElementsMounted: autoUpdate,
    open,
    onOpenChange: value => {
      if (!value) {
        onClose?.();
      }
    }
  });
  const dismiss = useDismiss(context);
  const role = useRole(context);
  const {
    getReferenceProps,
    getFloatingProps
  } = useInteractions([role, dismiss]);
  const [floatingKey, setFloatingKey] = useState(0);
  const {
    theme,
    screenMode
  } = useTheme();
  return /*#__PURE__*/jsxs(Fragment, {
    children: [open ? /*#__PURE__*/jsx(PreventScroll, {}) : null, renderTrigger({
      ref: refs.setReference,
      getInteractionProps: getReferenceProps
    }), /*#__PURE__*/jsx(FloatingPortal, {
      children: /*#__PURE__*/jsx(ThemeProvider, {
        theme: "personal",
        screenMode: theme === 'personal' ? screenMode : 'light',
        isNotRootProvider: true,
        children: /*#__PURE__*/jsx(Transition, {
          show: open,
          leave: "transition-opacity",
          leaveTo: "opacity-0",
          beforeEnter: () => {
            setFloatingKey(prev => prev + 1);
          },
          afterLeave: onCloseEnd,
          children: /*#__PURE__*/jsx(FocusScope, {
            children: /*#__PURE__*/jsx(FloatingFocusManager, {
              context: context,
              children: /*#__PURE__*/jsx("div", {
                // Force inner state invalidation on open
                ref: refs.setFloating,
                className: clsx('np-popover-v2-container', {
                  'np-popover-v2-container--size-md': size$1 === 'md',
                  'np-popover-v2-container--size-lg': size$1 === 'lg'
                })
                // eslint-disable-next-line react/forbid-dom-props
                ,
                style: floatingStyles,
                ...getFloatingProps(),
                children: /*#__PURE__*/jsxs("div", {
                  className: clsx('np-popover-v2', title && 'np-popover-v2--has-title', {
                    'np-popover-v2--padding-md': padding === 'md'
                  }),
                  children: [title ? /*#__PURE__*/jsx("h2", {
                    className: "np-popover-v2-title np-text-title-body",
                    children: title
                  }) : null, /*#__PURE__*/jsx("div", {
                    className: "np-popover-v2-content np-text-body-default",
                    children: children
                  })]
                })
              }, floatingKey)
            })
          })
        })
      })
    })]
  });
}

export { Popover };
//# sourceMappingURL=_Popover.mjs.map
