import PropTypes from 'prop-types';
import { useState, createElement as _createElement } from 'react';
import MoreHorizIcon from '@mui/icons-material/MoreHoriz';
import { Box, Divider, IconButton, ListItemIcon, ListItemText, Menu, MenuItem, Tooltip } from '@mui/material';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export default function Actions({
  actions = [],
  size = 'small',
  ...props
}) {
  const [anchorEl, setAnchorEl] = useState(null);
  const open = Boolean(anchorEl);
  const onOpen = e => {
    try {
      e.stopPropagation();
      e.preventDefault();
      // eslint-disable-next-line no-empty
    } catch {}
    setAnchorEl(e.currentTarget);
  };
  const onClose = e => {
    try {
      e.stopPropagation();
      e.preventDefault();
      // eslint-disable-next-line no-empty
    } catch {}
    setAnchorEl(null);
  };
  return /*#__PURE__*/_jsxs(Box, {
    ...props,
    children: [/*#__PURE__*/_jsx(IconButton, {
      "aria-label": "more",
      "aria-controls": "actions-menu",
      "aria-haspopup": "true",
      "data-cy": "actions-menu-icon",
      onClick: onOpen,
      size: size,
      children: /*#__PURE__*/_jsx(MoreHorizIcon, {
        fontSize: size
      })
    }), /*#__PURE__*/_jsx(Menu, {
      id: "actions-menu",
      "data-cy": "actions-menu",
      anchorEl: anchorEl,
      keepMounted: true,
      open: open,
      onClose: onClose,
      slotProps: {
        paper: {}
      },
      children: actions.map((action, index) => {
        if (typeof action === 'function') {
          return action({
            close: onClose
          });
        }
        if (action.separator) {
          // eslint-disable-next-line react/no-array-index-key
          return /*#__PURE__*/_jsx(Divider, {}, `separator-${index}`);
        }
        const {
          icon,
          text,
          render,
          onClick,
          disabled = false,
          tip,
          ...opts
        } = action;
        const item = render && typeof render === 'function' ? render({
          close: onClose
        }) : /*#__PURE__*/_createElement(MenuItem, {
          ...opts,
          disabled: disabled,
          dense: true,
          onClick: async e => {
            e.stopPropagation();
            await onClick?.(e);
            onClose();
          }
          // eslint-disable-next-line react/no-array-index-key
          ,
          key: index,
          "data-cy": `actions-menu-${index}`
        }, /*#__PURE__*/_jsx(ListItemIcon, {
          style: {
            minWidth: 24,
            marginRight: 8
          },
          children: icon
        }), /*#__PURE__*/_jsx(ListItemText, {
          primary: text
        }));
        return tip ? /*#__PURE__*/_jsx(Tooltip, {
          title: tip,
          children: /*#__PURE__*/_jsx("span", {
            children: item
          })
        }) : item;
      })
    })]
  });
}
Actions.propTypes = {
  actions: PropTypes.array,
  size: PropTypes.oneOf(['small', 'medium', 'large'])
};