import { useFloating, autoUpdate, offset, flip, arrow } from '@floating-ui/react';
import { clsx } from 'clsx';
import { useState, useRef, useId, cloneElement } from 'react';
import '../common/theme.mjs';
import '../common/direction.mjs';
import '@transferwise/neptune-tokens';
import '../common/propsValues/control.mjs';
import '../common/propsValues/size.mjs';
import '../common/propsValues/typography.mjs';
import '../common/propsValues/width.mjs';
import '../common/propsValues/type.mjs';
import '../common/propsValues/dateMode.mjs';
import '../common/propsValues/monthFormat.mjs';
import { Position } from '../common/propsValues/position.mjs';
import '../common/propsValues/layouts.mjs';
import '../common/propsValues/status.mjs';
import '../common/propsValues/sentiment.mjs';
import '../common/propsValues/profileType.mjs';
import '../common/propsValues/variant.mjs';
import '../common/propsValues/scroll.mjs';
import '../common/propsValues/markdownNodeType.mjs';
import '../common/fileType.mjs';
import '@transferwise/formatting';
import '@transferwise/icons';
import 'react-intl';
import '../common/closeButton/CloseButton.messages.mjs';
import { jsxs, jsx } from 'react/jsx-runtime';

const Tooltip = ({
  position = Position.TOP,
  children = undefined,
  label,
  id,
  className
}) => {
  const [open, setOpen] = useState(false);
  const arrowRef = useRef(null);
  const fallbackId = useId();
  const tooltipId = id ?? fallbackId;
  const {
    refs,
    floatingStyles,
    placement,
    middlewareData
  } = useFloating({
    placement: position,
    middleware: [offset(16), flip({
      fallbackPlacements: [Position.TOP]
    }),
    // eslint-disable-next-line react-hooks/refs -- arrowRef is passed to floating-ui middleware, legitimate API usage
    arrow({
      element: arrowRef,
      padding: 8
    })],
    whileElementsMounted: open ? autoUpdate : undefined,
    open
  });
  const arrowStyle = {
    ...(middlewareData.arrow?.x != null ? {
      left: middlewareData.arrow.x
    } : {}),
    ...(middlewareData.arrow?.y != null ? {
      top: middlewareData.arrow.y
    } : {})
  };
  return /*#__PURE__*/ /* eslint-disable react-hooks/refs -- setReference and setFloating are callback refs from floating-ui, safe to pass during render */jsxs("span", {
    ref: refs.setReference,
    className: "tw-tooltip-container",
    onMouseOver: () => setOpen(true),
    onFocus: () => setOpen(true),
    onMouseOut: () => setOpen(false),
    onBlur: () => setOpen(false),
    children: [children ? /*#__PURE__*/cloneElement(children, {
      'aria-describedby': `${tooltipId}-tooltip`
    }) : null, /*#__PURE__*/jsx("div", {
      ref: refs.setFloating,
      className: clsx('np-tooltip', 'np-panel', open ? `np-panel--open np-tooltip--open` : null, className),
      style: floatingStyles,
      "data-popper-placement": placement,
      "aria-hidden": !open,
      role: "tooltip",
      id: `${tooltipId}-tooltip`,
      children: /*#__PURE__*/jsxs("div", {
        className: "np-panel__content tooltip-inner",
        children: [label, /*#__PURE__*/jsx("div", {
          ref: arrowRef,
          className: clsx('np-panel__arrow'),
          style: arrowStyle
        })]
      })
    })]
  })
  /* eslint-enable react-hooks/refs */;
};

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