import { clsx } from 'clsx';
import { useState, useRef, useId, useEffect, cloneElement } from 'react';
import { usePopper } from 'react-popper';
import '../common/theme.mjs';
import '../common/direction.mjs';
import '../common/propsValues/control.mjs';
import '../common/propsValues/breakpoint.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/icons';
import 'react-intl';
import '../common/closeButton/CloseButton.messages.mjs';
import { jsxs, jsx } from 'react/jsx-runtime';

/* eslint-disable @typescript-eslint/ban-ts-comment */
const Tooltip = ({
  position = Position.TOP,
  children = undefined,
  label,
  id,
  className
}) => {
  const [open, setOpen] = useState(false);
  const anchorReference = useRef(null);
  const [arrowElement, setArrowElement] = useState(null);
  const [popperElement, setPopperElement] = useState(null);
  const fallbackId = useId();
  const tooltipId = id ?? fallbackId;
  const modifiers = [];
  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: [0, 16]
    }
  });
  modifiers.push({
    name: 'flip',
    options: {
      fallbackPlacements: Position.TOP
    }
  });
  const {
    styles,
    attributes,
    forceUpdate
  } = usePopper(anchorReference.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]);
  return /*#__PURE__*/jsxs("span", {
    ref: anchorReference,
    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", {
      // @ts-expect-error
      ref: setPopperElement,
      className: clsx('np-tooltip', 'np-panel', open ? `np-panel--open np-tooltip--open` : null, className),
      style: {
        ...styles.popper
      },
      ...attributes.popper,
      "aria-hidden": !open,
      role: "tooltip",
      id: `${tooltipId}-tooltip`,
      children: /*#__PURE__*/jsxs("div", {
        className: "np-panel__content tooltip-inner",
        children: [label, /*#__PURE__*/jsx("div", {
          // @ts-expect-error
          ref: setArrowElement,
          className: clsx('np-panel__arrow'),
          style: styles.arrow
        })]
      })
    })]
  });
};

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