'use strict';

var React = require('react');
var ui_ContextMenu = require('./ContextMenu.js');
var ui_Icon = require('./Icon.js');
var ui_IconButton = require('./IconButton.js');
var index = require('../index-1b3c69da.js');
var LocalizationContext = require('../LocalizationContext-44d2f61b.js');
var types = require('../types-df5bd4e5.js');
require('../_rollupPluginBabelHelpers-21ae158b.js');
require('react-dom');
require('./SortByRow.js');
require('../uuid-dee74248.js');
require('../index-2bfd366f.js');
require('prop-types');
require('../stringSet-7aebf034.js');
require('../utils/message/getOutgoingMessageState.js');
require('../index-88e8fdfa.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

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
  } = React.useContext(LocalizationContext.LocalizationContext);
  const triggerRef = React.useRef(null);
  const containerRef = React.useRef(null);
  const showMenuItemCopy = index.isUserMessage(message);
  const showMenuItemEdit = !(channel !== null && channel !== void 0 && channel.isEphemeral) && index.isUserMessage(message) && index.isSentMessage(message) && isByMe;
  const showMenuItemResend = index.isFailedMessage(message) && (message === null || message === void 0 ? void 0 : message.isResendable) && isByMe;
  const showMenuItemDelete = !(channel !== null && channel !== void 0 && channel.isEphemeral) && !index.isPendingMessage(message) && isByMe;
  const showMenuItemOpenInChannel = onMoveToParentMessage !== null;
  /**
   * TODO: Manage timing issue
   * User delete pending message -> Sending message success
   */
  const isReplyTypeEnabled = !index.isFailedMessage(message) && !index.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) === types.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["default"].createElement("div", {
    className: index.getClassName([className, 'sendbird-message-item-menu']),
    ref: containerRef
  }, /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu["default"], {
    menuTrigger: toggleDropdown => /*#__PURE__*/React__default["default"].createElement(ui_IconButton, {
      className: "sendbird-message-item-menu__trigger",
      ref: triggerRef,
      width: "32px",
      height: "32px",
      onClick: () => {
        toggleDropdown();
        setSupposedHover(true);
      },
      onBlur: () => {
        setSupposedHover(false);
      }
    }, /*#__PURE__*/React__default["default"].createElement(ui_Icon["default"], {
      className: "sendbird-message-item-menu__trigger__icon",
      type: ui_Icon.IconTypes.MORE,
      fillColor: ui_Icon.IconColors.CONTENT_INVERSE,
      width: "24px",
      height: "24px"
    })),
    menuItems: close => {
      var _message$threadInfo;
      const closeDropdown = () => {
        close();
        setSupposedHover(false);
      };
      return /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.MenuItems, {
        className: "sendbird-message-item-menu__list",
        parentRef: triggerRef,
        parentContainRef: containerRef,
        closeDropdown: closeDropdown,
        openLeft: isByMe
      }, showMenuItemCopy && /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.MenuItem, {
        className: "sendbird-message-item-menu__list__menu-item menu-item-copy",
        onClick: () => {
          index.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["default"].createElement(ui_ContextMenu.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["default"].createElement(ui_ContextMenu.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["default"].createElement(ui_ContextMenu.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["default"].createElement(ui_ContextMenu.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["default"].createElement(ui_ContextMenu.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["default"].createElement(ui_ContextMenu.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));
    }
  }));
}

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