'use strict';

var clsx = require('clsx');
var React = require('react');
var Title = require('../title/Title.js');
var jsxRuntime = require('react/jsx-runtime');
var logActionRequired = require('../utilities/logActionRequired.js');
var ResponsivePanel = require('../common/responsivePanel/ResponsivePanel.js');
var typography = require('../common/propsValues/typography.js');
var position = require('../common/propsValues/position.js');

function resolvePlacement(preferredPlacement) {
  switch (preferredPlacement) {
    case 'left-top':
    case 'right-top':
      return 'top';
    case 'bottom-left':
    case 'bottom-right':
      return 'bottom';
    default:
      return preferredPlacement;
  }
}
function Popover({
  children,
  className,
  content,
  preferredPlacement = position.Position.RIGHT,
  title,
  onClose,
  'aria-label': ariaLabel
}) {
  const titleId = React.useId();
  const resolvedPlacement = resolvePlacement(preferredPlacement);
  React.useEffect(() => {
    if (resolvedPlacement !== preferredPlacement) {
      logActionRequired.logActionRequired(`Popover has deprecated ${preferredPlacement} value for the 'preferredPlacement' prop. Please use ${resolvedPlacement} instead.`);
    }
  }, [preferredPlacement, resolvedPlacement]);
  const anchorReference = React.useRef(null);
  const [open, setOpen] = React.useState(false);
  const handleOnClose = () => {
    setOpen(false);
    onClose?.();
  };
  return /*#__PURE__*/jsxRuntime.jsxs("span", {
    className: clsx.clsx('np-popover', className),
    children: [/*#__PURE__*/jsxRuntime.jsx("span", {
      ref: anchorReference,
      className: "d-inline-block",
      children: /*#__PURE__*/React.isValidElement(children) ? /*#__PURE__*/React.cloneElement(children, {
        onClick: () => {
          children.props.onClick?.();
          setOpen(prevOpen => !prevOpen);
        }
      }) : children
    }), /*#__PURE__*/jsxRuntime.jsx(ResponsivePanel, {
      "aria-label": ariaLabel,
      "aria-labelledby": title && !ariaLabel ? titleId : undefined,
      open: open,
      anchorRef: anchorReference,
      position: resolvedPlacement,
      arrow: true,
      className: "np-popover__container",
      onClose: handleOnClose,
      children: /*#__PURE__*/jsxRuntime.jsxs("div", {
        className: "np-popover__content np-text-default-body",
        children: [title && /*#__PURE__*/jsxRuntime.jsx(Title, {
          type: typography.Typography.TITLE_BODY,
          id: titleId,
          className: "m-b-1",
          children: title
        }), content]
      })
    })]
  });
}

module.exports = Popover;
//# sourceMappingURL=Popover.js.map
