import React__default, { useContext, useRef } from 'react';
import ContextMenu, { MenuItems, MenuItem } from './ContextMenu.js';
import Icon, { IconTypes, IconColors } from './Icon.js';
import IconButton from './IconButton.js';
import { q as isUserMessage, C as isSentMessage, D as isFailedMessage, E as isPendingMessage, h as getClassName, F as copyToClipboard } from '../index-eefb44ba.js';
import { a as LocalizationContext } from '../LocalizationContext-b2390bf8.js';
import { R as Role } from '../types-234b17b9.js';
import '../_rollupPluginBabelHelpers-4f8ebde4.js';
import 'react-dom';
import './SortByRow.js';
import '../uuid-a79b5ba8.js';
import '../index-4372fcec.js';
import 'prop-types';
import '../stringSet-eefbc798.js';
import '../utils/message/getOutgoingMessageState.js';
import '../index-67135027.js';

function MessageItemMenu(_ref) {
  var _channel$isGroupChann;
  let {
    className,
    message,
    channel,
    isByMe = false,
    disabled = false,
    replyType,
    disableDeleteMessage = null,
    showEdit,
    showRemove,
    resendMessage,
    setQuoteMessage,
    setSupposedHover,
    onReplyInThread,
    onMoveToParentMessage = null
  } = _ref;
  const {
    stringSet
  } = useContext(LocalizationContext);
  const triggerRef = useRef(null);
  const containerRef = useRef(null);
  const showMenuItemCopy = isUserMessage(message);
  const showMenuItemEdit = !(channel !== null && channel !== void 0 && channel.isEphemeral) && isUserMessage(message) && isSentMessage(message) && isByMe;
  const showMenuItemResend = isFailedMessage(message) && (message === null || message === void 0 ? void 0 : message.isResendable) && isByMe;
  const showMenuItemDelete = !(channel !== null && channel !== void 0 && channel.isEphemeral) && !isPendingMessage(message) && isByMe;
  const showMenuItemOpenInChannel = onMoveToParentMessage !== null;
  /**
   * TODO: Manage timing issue
   * User delete pending message -> Sending message success
   */
  const isReplyTypeEnabled = !isFailedMessage(message) && !isPendingMessage(message) && (channel === null || channel === void 0 ? void 0 : (_channel$isGroupChann = channel.isGroupChannel) === null || _channel$isGroupChann === void 0 ? void 0 : _channel$isGroupChann.call(channel)) && !(channel !== null && channel !== void 0 && channel.isEphemeral) && ((channel === null || channel === void 0 ? void 0 : channel.isBroadcast) && (channel === null || channel === void 0 ? void 0 : channel.myRole) === Role.OPERATOR || !(channel !== null && channel !== void 0 && channel.isBroadcast));
  const showMenuItemReply = isReplyTypeEnabled && replyType === 'QUOTE_REPLY';
  const showMenuItemThread = isReplyTypeEnabled && replyType === 'THREAD' && !(message !== null && message !== void 0 && message.parentMessageId) && onReplyInThread;
  if (!(showMenuItemCopy || showMenuItemReply || showMenuItemThread || showMenuItemOpenInChannel || showMenuItemEdit || showMenuItemResend || showMenuItemDelete)) {
    return null;
  }
  return /*#__PURE__*/React__default.createElement("div", {
    className: getClassName([className, 'sendbird-message-item-menu']),
    ref: containerRef
  }, /*#__PURE__*/React__default.createElement(ContextMenu, {
    menuTrigger: toggleDropdown => /*#__PURE__*/React__default.createElement(IconButton, {
      className: "sendbird-message-item-menu__trigger",
      ref: triggerRef,
      width: "32px",
      height: "32px",
      onClick: () => {
        toggleDropdown();
        setSupposedHover(true);
      },
      onBlur: () => {
        setSupposedHover(false);
      }
    }, /*#__PURE__*/React__default.createElement(Icon, {
      className: "sendbird-message-item-menu__trigger__icon",
      type: IconTypes.MORE,
      fillColor: IconColors.CONTENT_INVERSE,
      width: "24px",
      height: "24px"
    })),
    menuItems: close => {
      var _message$threadInfo;
      const closeDropdown = () => {
        close();
        setSupposedHover(false);
      };
      return /*#__PURE__*/React__default.createElement(MenuItems, {
        className: "sendbird-message-item-menu__list",
        parentRef: triggerRef,
        parentContainRef: containerRef,
        closeDropdown: closeDropdown,
        openLeft: isByMe
      }, showMenuItemCopy && /*#__PURE__*/React__default.createElement(MenuItem, {
        className: "sendbird-message-item-menu__list__menu-item menu-item-copy",
        onClick: () => {
          copyToClipboard(message === null || message === void 0 ? void 0 : message.message);
          closeDropdown();
        },
        dataSbId: "ui_message_item_menu_copy"
      }, stringSet.MESSAGE_MENU__COPY), showMenuItemReply && /*#__PURE__*/React__default.createElement(MenuItem, {
        className: "sendbird-message-item-menu__list__menu-item menu-item-reply",
        onClick: () => {
          setQuoteMessage(message);
          closeDropdown();
        },
        disable: (message === null || message === void 0 ? void 0 : message.parentMessageId) > 0,
        dataSbId: "ui_message_item_menu_reply"
      }, stringSet.MESSAGE_MENU__REPLY), showMenuItemThread && /*#__PURE__*/React__default.createElement(MenuItem, {
        className: "sendbird-message-item-menu__list__menu-item menu-item-thread",
        onClick: () => {
          onReplyInThread === null || onReplyInThread === void 0 ? void 0 : onReplyInThread({
            message
          });
          closeDropdown();
        },
        dataSbId: "ui_message_item_menu_thread"
      }, stringSet.MESSAGE_MENU__THREAD), showMenuItemOpenInChannel && /*#__PURE__*/React__default.createElement(MenuItem, {
        className: "sendbird-message-item-menu__list__menu-item menu-item-open-channel",
        onClick: () => {
          onMoveToParentMessage === null || onMoveToParentMessage === void 0 ? void 0 : onMoveToParentMessage();
          closeDropdown();
        },
        dataSbId: "ui_message_item_menu_open_in_channel"
      }, stringSet.MESSAGE_MENU__OPEN_IN_CHANNEL), showMenuItemEdit && /*#__PURE__*/React__default.createElement(MenuItem, {
        className: "sendbird-message-item-menu__list__menu-item menu-item-edit",
        onClick: () => {
          if (!disabled) {
            showEdit(true);
            closeDropdown();
          }
        },
        dataSbId: "ui_message_item_menu_edit"
      }, stringSet.MESSAGE_MENU__EDIT), showMenuItemResend && /*#__PURE__*/React__default.createElement(MenuItem, {
        className: "sendbird-message-item-menu__list__menu-item menu-item-resend",
        onClick: () => {
          if (!disabled) {
            resendMessage(message);
            closeDropdown();
          }
        },
        dataSbId: "ui_message_item_menu_resend"
      }, stringSet.MESSAGE_MENU__RESEND), showMenuItemDelete && /*#__PURE__*/React__default.createElement(MenuItem, {
        className: "sendbird-message-item-menu__list__menu-item menu-item-delete",
        onClick: () => {
          if (!disabled) {
            showRemove(true);
            closeDropdown();
          }
        },
        disable: typeof disableDeleteMessage === 'boolean' ? disableDeleteMessage : (message === null || message === void 0 ? void 0 : (_message$threadInfo = message.threadInfo) === null || _message$threadInfo === void 0 ? void 0 : _message$threadInfo.replyCount) > 0,
        dataSbId: "ui_message_item_menu_delete"
      }, stringSet.MESSAGE_MENU__DELETE));
    }
  }));
}

export { MessageItemMenu as default };
//# sourceMappingURL=MessageItemMenu.js.map
