'use strict';

var _rollupPluginBabelHelpers = require('../_rollupPluginBabelHelpers-acf412a3.js');
var React = require('react');
var index$1 = require('../index-3fc4f7d7.js');
var ui_Avatar = require('./Avatar.js');
var ui_UserProfile = require('./UserProfile.js');
var ui_MessageStatus = require('../index-0a0a6c08.js');
var ui_MessageItemMenu = require('./MessageItemMenu.js');
var ui_MessageItemReactionMenu = require('./MessageItemReactionMenu.js');
var ui_ContextMenu = require('./ContextMenu.js');
var ui_Label = require('../index-35633fb6.js');
var ui_EmojiReactions = require('./EmojiReactions.js');
var ui_AdminMessage = require('./AdminMessage.js');
var ui_TextMessageItemBody = require('./TextMessageItemBody.js');
var ui_FileMessageItemBody = require('./FileMessageItemBody.js');
var ui_ThumbnailMessageItemBody = require('./ThumbnailMessageItemBody.js');
var ui_OGMessageItemBody = require('./OGMessageItemBody.js');
var ui_UnknownMessageItemBody = require('./UnknownMessageItemBody.js');
var ui_QuoteMessage = require('./QuoteMessage.js');
var index = require('../index-a91b85d9.js');
var UserProfileContext = require('../UserProfileContext-d993c96b.js');
var LocalizationContext = require('../LocalizationContext-8cc9717d.js');
var useSendbirdStateContext = require('../useSendbirdStateContext.js');
var useLongPress = require('../useLongPress-cf56e64a.js');
var index$2 = require('../index-ff7b4cb6.js');
var MediaQueryContext = require('../MediaQueryContext-fad211ad.js');
var ui_ThreadReplies = require('./ThreadReplies.js');
var _const = require('../const-a3e169f4.js');
var ui_VoiceMessageItemBody = require('./VoiceMessageItemBody.js');
var utils = require('../utils-5aa0708e.js');
require('../index-d749c4c2.js');
require('./ImageRenderer.js');
require('./Icon.js');
require('prop-types');
require('../uuid-1eceb0c8.js');
require('../sendbirdSelectors.js');
require('../topics-e24ef339.js');
require('./Button.js');
require('../stringSet-ce782136.js');
require('./Loader.js');
require('../utils/message/getOutgoingMessageState.js');
require('../index-0e24aca4.js');
require('../index-bf8f9551.js');
require('./IconButton.js');
require('../types-9466af03.js');
require('./ReactionButton.js');
require('react-dom');
require('./SortByRow.js');
require('./ReactionBadge.js');
require('./BottomSheet.js');
require('../index-8029e07a.js');
require('./UserListItem.js');
require('./MutedAvatarOverlay.js');
require('./Checkbox.js');
require('./Tooltip.js');
require('./TooltipWrapper.js');
require('../Message/context.js');
require('../tokenize-88b021e0.js');
require('../index-db8116c0.js');
require('../index.module-e3392caa.js');
require('./MentionLabel.js');
require('./LinkLabel.js');
require('../consts-016f07ac.js');
require('./TextButton.js');
require('../color-5cfb41c7.js');
require('../consts-eb163887.js');
require('../withSendbird.js');
require('./ProgressBar.js');
require('../VoicePlayer/useVoicePlayer.js');
require('../index-90711a57.js');
require('../consts-9641990e.js');
require('../VoiceRecorder/context.js');
require('./Modal.js');
require('./PlaybackTime.js');

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

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

// should initialize in UserProfileContext.jsx

function MessageContent(_ref) {
  var _sender, _message$threadInfo, _message$isAdminMessa, _channel$members2, _channel$members2$fin, _channel$joinedAt, _message$parentMessag2, _message$parentMessag3, _channel$url, _message$reactions, _message$isUserMessag, _message$isFileMessag;
  let {
    className,
    userId,
    channel,
    message,
    disabled = false,
    chainTop = false,
    chainBottom = false,
    isReactionEnabled = false,
    disableQuoteMessage = false,
    replyType,
    threadReplySelectType,
    nicknamesMap,
    emojiContainer,
    scrollToMessage,
    showEdit,
    showRemove,
    showFileViewer,
    resendMessage,
    toggleReaction,
    setQuoteMessage,
    onReplyInThread,
    onQuoteMessageClick,
    onMessageHeightChange
  } = _ref;
  const messageTypes = index.getUIKitMessageTypes();
  const {
    dateLocale
  } = LocalizationContext.useLocalization();
  const {
    config
  } = (useSendbirdStateContext === null || useSendbirdStateContext === void 0 ? void 0 : useSendbirdStateContext()) || {};
  const {
    disableUserProfile,
    renderUserProfile
  } = React.useContext(UserProfileContext.UserProfileContext);
  const avatarRef = React.useRef(null);
  const contentRef = React.useRef(null);
  const {
    isMobile
  } = MediaQueryContext.useMediaQueryContext();
  const [showMenu, setShowMenu] = React.useState(false);
  const [mouseHover, setMouseHover] = React.useState(false);
  const [supposedHover, setSupposedHover] = React.useState(false);
  const isByMe = userId === (message === null || message === void 0 ? void 0 : (_sender = message.sender) === null || _sender === void 0 ? void 0 : _sender.userId) || (message === null || message === void 0 ? void 0 : message.sendingStatus) === 'pending' || (message === null || message === void 0 ? void 0 : message.sendingStatus) === 'failed';
  const isByMeClassName = isByMe ? 'outgoing' : 'incoming';
  const chainTopClassName = chainTop ? 'chain-top' : '';
  const isReactionEnabledInChannel = isReactionEnabled && !(channel !== null && channel !== void 0 && channel.isEphemeral);
  const isReactionEnabledClassName = isReactionEnabledInChannel ? 'use-reactions' : '';
  const supposedHoverClassName = supposedHover ? 'sendbird-mouse-hover' : '';
  const useReplying = !!((replyType === 'QUOTE_REPLY' || replyType === 'THREAD') && message !== null && message !== void 0 && message.parentMessageId && message !== null && message !== void 0 && message.parentMessage && !disableQuoteMessage);
  const useReplyingClassName = useReplying ? 'use-quote' : '';
  const isOgMessageEnabledInGroupChannel = (channel === null || channel === void 0 ? void 0 : channel.isGroupChannel()) && config.groupChannel.enableOgtag;

  // Thread replies
  const displayThreadReplies = (message === null || message === void 0 ? void 0 : (_message$threadInfo = message.threadInfo) === null || _message$threadInfo === void 0 ? void 0 : _message$threadInfo.replyCount) > 0 && replyType === 'THREAD';

  // onMouseDown: (e: React.MouseEvent<T>) => void;
  // onTouchStart: (e: React.TouchEvent<T>) => void;
  // onMouseUp: (e: React.MouseEvent<T>) => void;
  // onMouseLeave: (e: React.MouseEvent<T>) => void;
  // onTouchEnd: (e: React.TouchEvent<T>) => void;
  const longPress = useLongPress.useLongPress({
    onLongPress: () => {
      if (isMobile) {
        setShowMenu(true);
      }
    },
    onClick: utils.noop
  }, {
    delay: 300,
    shouldPreventDefault: false
  });
  if (message !== null && message !== void 0 && (_message$isAdminMessa = message.isAdminMessage) !== null && _message$isAdminMessa !== void 0 && _message$isAdminMessa.call(message) || (message === null || message === void 0 ? void 0 : message.messageType) === 'admin') {
    return /*#__PURE__*/React__default["default"].createElement(ui_AdminMessage, {
      message: message
    });
  }
  return /*#__PURE__*/React__default["default"].createElement("div", {
    className: index.getClassName([className, 'sendbird-message-content', isByMeClassName]),
    onMouseOver: () => setMouseHover(true),
    onMouseLeave: () => setMouseHover(false)
  }, /*#__PURE__*/React__default["default"].createElement("div", {
    className: index.getClassName(['sendbird-message-content__left', isReactionEnabledClassName, isByMeClassName, useReplyingClassName])
  }, !isByMe && !chainBottom &&
  /*#__PURE__*/
  /** user profile */
  React__default["default"].createElement(ui_ContextMenu["default"], {
    menuTrigger: toggleDropdown => {
      var _channel$members, _channel$members$find, _message$sender2;
      return /*#__PURE__*/React__default["default"].createElement(ui_Avatar["default"], {
        className: `sendbird-message-content__left__avatar ${displayThreadReplies ? 'use-thread-replies' : ''}`
        // @ts-ignore
        ,
        src: (channel === null || channel === void 0 ? void 0 : (_channel$members = channel.members) === null || _channel$members === void 0 ? void 0 : (_channel$members$find = _channel$members.find(member => {
          var _message$sender;
          return (member === null || member === void 0 ? void 0 : member.userId) === (message === null || message === void 0 ? void 0 : (_message$sender = message.sender) === null || _message$sender === void 0 ? void 0 : _message$sender.userId);
        })) === null || _channel$members$find === void 0 ? void 0 : _channel$members$find.profileUrl) || (message === null || message === void 0 ? void 0 : (_message$sender2 = message.sender) === null || _message$sender2 === void 0 ? void 0 : _message$sender2.profileUrl) || ''
        // TODO: Divide getting profileUrl logic to utils
        ,
        ref: avatarRef,
        width: "28px",
        height: "28px",
        onClick: () => {
          if (!disableUserProfile) toggleDropdown();
        }
      });
    },
    menuItems: closeDropdown => /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.MenuItems
    /**
    * parentRef: For catching location(x, y) of MenuItems
    * parentContainRef: For toggling more options(menus & reactions)
    */, {
      parentRef: avatarRef,
      parentContainRef: avatarRef,
      closeDropdown: closeDropdown,
      style: {
        paddingTop: '0px',
        paddingBottom: '0px'
      }
    }, renderUserProfile
    // @ts-ignore
    ? renderUserProfile({
      user: message === null || message === void 0 ? void 0 : message.sender,
      close: closeDropdown
    })
    // @ts-ignore
    : /*#__PURE__*/React__default["default"].createElement(ui_UserProfile, {
      user: message.sender,
      onSuccess: closeDropdown
    }))
  }), isByMe && !isMobile && /*#__PURE__*/React__default["default"].createElement("div", {
    className: index.getClassName(['sendbird-message-content-menu', isReactionEnabledClassName, supposedHoverClassName, isByMeClassName])
  }, /*#__PURE__*/React__default["default"].createElement(ui_MessageItemMenu, {
    className: "sendbird-message-content-menu__normal-menu",
    channel: channel,
    message: message,
    isByMe: isByMe,
    replyType: replyType,
    disabled: disabled,
    showEdit: showEdit,
    showRemove: showRemove,
    resendMessage: resendMessage,
    setQuoteMessage: setQuoteMessage,
    setSupposedHover: setSupposedHover,
    onReplyInThread: _ref2 => {
      let {
        message
      } = _ref2;
      if (threadReplySelectType === _const.ThreadReplySelectType.THREAD) {
        onReplyInThread({
          message
        });
      } else if (threadReplySelectType === _const.ThreadReplySelectType.PARENT) {
        var _message$parentMessag;
        scrollToMessage((_message$parentMessag = message.parentMessage) === null || _message$parentMessag === void 0 ? void 0 : _message$parentMessag.createdAt, message.parentMessageId);
      }
    }
  }), isReactionEnabledInChannel && /*#__PURE__*/React__default["default"].createElement(ui_MessageItemReactionMenu, {
    className: "sendbird-message-content-menu__reaction-menu",
    message: message,
    userId: userId,
    spaceFromTrigger: {},
    emojiContainer: emojiContainer,
    toggleReaction: toggleReaction,
    setSupposedHover: setSupposedHover
  }))), /*#__PURE__*/React__default["default"].createElement("div", _rollupPluginBabelHelpers._extends({
    className: "sendbird-message-content__middle"
  }, isMobile ? _rollupPluginBabelHelpers._objectSpread2({}, longPress) : {}, {
    ref: contentRef
  }), !isByMe && !chainTop && !useReplying && /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    className: "sendbird-message-content__middle__sender-name",
    type: ui_Label.LabelTypography.CAPTION_2,
    color: ui_Label.LabelColors.ONBACKGROUND_2
  },
  // @ts-ignore
  (channel === null || channel === void 0 ? void 0 : (_channel$members2 = channel.members) === null || _channel$members2 === void 0 ? void 0 : (_channel$members2$fin = _channel$members2.find(member => {
    var _message$sender3;
    return (member === null || member === void 0 ? void 0 : member.userId) === (message === null || message === void 0 ? void 0 : (_message$sender3 = message.sender) === null || _message$sender3 === void 0 ? void 0 : _message$sender3.userId);
  })) === null || _channel$members2$fin === void 0 ? void 0 : _channel$members2$fin.nickname) || index.getSenderName(message)
  // TODO: Divide getting profileUrl logic to utils
  ), useReplying ? /*#__PURE__*/React__default["default"].createElement("div", {
    className: index.getClassName(['sendbird-message-content__middle__quote-message', isByMe ? 'outgoing' : 'incoming', useReplyingClassName])
  }, /*#__PURE__*/React__default["default"].createElement(ui_QuoteMessage, {
    className: "sendbird-message-content__middle__quote-message__quote",
    message: message,
    userId: userId,
    isByMe: isByMe,
    isUnavailable: replyType === 'THREAD' && ((_channel$joinedAt = channel === null || channel === void 0 ? void 0 : channel.joinedAt) !== null && _channel$joinedAt !== void 0 ? _channel$joinedAt : 0 * 1000) > ((_message$parentMessag2 = (_message$parentMessag3 = message.parentMessage) === null || _message$parentMessag3 === void 0 ? void 0 : _message$parentMessag3.createdAt) !== null && _message$parentMessag2 !== void 0 ? _message$parentMessag2 : 0),
    onClick: () => {
      var _message$parentMessag4;
      if (replyType === 'THREAD' && threadReplySelectType === _const.ThreadReplySelectType.THREAD) {
        onQuoteMessageClick === null || onQuoteMessageClick === void 0 ? void 0 : onQuoteMessageClick({
          message: message
        });
      }
      if ((replyType === 'QUOTE_REPLY' || replyType === 'THREAD' && threadReplySelectType === _const.ThreadReplySelectType.PARENT) && message !== null && message !== void 0 && (_message$parentMessag4 = message.parentMessage) !== null && _message$parentMessag4 !== void 0 && _message$parentMessag4.createdAt && message !== null && message !== void 0 && message.parentMessageId) {
        scrollToMessage(message.parentMessage.createdAt, message.parentMessageId);
      }
    }
  })) : null, /*#__PURE__*/React__default["default"].createElement("div", {
    className: index.getClassName(['sendbird-message-content__middle__body-container'])
  }, isByMe && !chainBottom && /*#__PURE__*/React__default["default"].createElement("div", {
    className: index.getClassName(['sendbird-message-content__middle__body-container__created-at', 'left', supposedHoverClassName])
  }, /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-message-content__middle__body-container__created-at__component-container"
  }, /*#__PURE__*/React__default["default"].createElement(ui_MessageStatus.MessageStatus, {
    message: message,
    channel: channel
  }))), isOgMessageEnabledInGroupChannel && index.isOGMessage(message) ? /*#__PURE__*/React__default["default"].createElement(ui_OGMessageItemBody, {
    className: "sendbird-message-content__middle__message-item-body",
    message: message,
    isByMe: isByMe,
    mouseHover: mouseHover,
    isMentionEnabled: (config === null || config === void 0 ? void 0 : config.isMentionEnabled) || false,
    isReactionEnabled: isReactionEnabledInChannel,
    onMessageHeightChange: onMessageHeightChange
  }) : index.isTextMessage(message) && /*#__PURE__*/React__default["default"].createElement(ui_TextMessageItemBody, {
    className: "sendbird-message-content__middle__message-item-body",
    message: message,
    isByMe: isByMe,
    mouseHover: mouseHover,
    isMentionEnabled: (config === null || config === void 0 ? void 0 : config.isMentionEnabled) || false,
    isReactionEnabled: isReactionEnabledInChannel
  }), index.getUIKitMessageType(message) === messageTypes.FILE && /*#__PURE__*/React__default["default"].createElement(ui_FileMessageItemBody, {
    className: "sendbird-message-content__middle__message-item-body",
    message: message,
    isByMe: isByMe,
    mouseHover: mouseHover,
    isReactionEnabled: isReactionEnabledInChannel
  }), index.isVoiceMessage(message) && /*#__PURE__*/React__default["default"].createElement(ui_VoiceMessageItemBody.VoiceMessageItemBody, {
    className: "sendbird-message-content__middle__message-item-body",
    message: message,
    channelUrl: (_channel$url = channel === null || channel === void 0 ? void 0 : channel.url) !== null && _channel$url !== void 0 ? _channel$url : '',
    isByMe: isByMe,
    isReactionEnabled: isReactionEnabledInChannel
  }), index.isThumbnailMessage(message) && /*#__PURE__*/React__default["default"].createElement(ui_ThumbnailMessageItemBody, {
    className: "sendbird-message-content__middle__message-item-body",
    message: message,
    isByMe: isByMe,
    mouseHover: mouseHover,
    isReactionEnabled: isReactionEnabledInChannel,
    showFileViewer: showFileViewer,
    style: isMobile ? {
      width: '100%'
    } : {}
  }), index.getUIKitMessageType(message) === messageTypes.UNKNOWN && /*#__PURE__*/React__default["default"].createElement(ui_UnknownMessageItemBody, {
    className: "sendbird-message-content__middle__message-item-body",
    message: message,
    isByMe: isByMe,
    mouseHover: mouseHover,
    isReactionEnabled: isReactionEnabledInChannel
  }), isReactionEnabledInChannel && (message === null || message === void 0 ? void 0 : (_message$reactions = message.reactions) === null || _message$reactions === void 0 ? void 0 : _message$reactions.length) > 0 && /*#__PURE__*/React__default["default"].createElement("div", {
    className: index.getClassName(['sendbird-message-content-reactions', !isByMe || index.isThumbnailMessage(message) || index.isOGMessage(message) ? '' : 'primary', mouseHover ? 'mouse-hover' : ''])
  }, /*#__PURE__*/React__default["default"].createElement(ui_EmojiReactions, {
    userId: userId,
    message: message,
    channel: channel,
    isByMe: isByMe,
    emojiContainer: emojiContainer,
    memberNicknamesMap: nicknamesMap,
    toggleReaction: toggleReaction
  })), !isByMe && !chainBottom && /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    className: index.getClassName(['sendbird-message-content__middle__body-container__created-at', 'right', supposedHoverClassName]),
    type: ui_Label.LabelTypography.CAPTION_3,
    color: ui_Label.LabelColors.ONBACKGROUND_2
  }, index$1.format((message === null || message === void 0 ? void 0 : message.createdAt) || 0, 'p', {
    locale: dateLocale
  }))), displayThreadReplies && /*#__PURE__*/React__default["default"].createElement(ui_ThreadReplies, {
    className: "sendbird-message-content__middle__thread-replies",
    threadInfo: message === null || message === void 0 ? void 0 : message.threadInfo,
    onClick: () => onReplyInThread === null || onReplyInThread === void 0 ? void 0 : onReplyInThread({
      message: message
    })
  })), /*#__PURE__*/React__default["default"].createElement("div", {
    className: index.getClassName(['sendbird-message-content__right', chainTopClassName, isReactionEnabledClassName, useReplyingClassName])
  }, !isByMe && !isMobile && /*#__PURE__*/React__default["default"].createElement("div", {
    className: index.getClassName(['sendbird-message-content-menu', chainTopClassName, supposedHoverClassName, isByMeClassName])
  }, isReactionEnabledInChannel && /*#__PURE__*/React__default["default"].createElement(ui_MessageItemReactionMenu, {
    className: "sendbird-message-content-menu__reaction-menu",
    message: message,
    userId: userId,
    spaceFromTrigger: {},
    emojiContainer: emojiContainer,
    toggleReaction: toggleReaction,
    setSupposedHover: setSupposedHover
  }), /*#__PURE__*/React__default["default"].createElement(ui_MessageItemMenu, {
    className: "sendbird-message-content-menu__normal-menu",
    channel: channel,
    message: message,
    isByMe: isByMe,
    replyType: replyType,
    disabled: disabled,
    showRemove: showRemove,
    resendMessage: resendMessage,
    setQuoteMessage: setQuoteMessage,
    setSupposedHover: setSupposedHover,
    onReplyInThread: _ref3 => {
      let {
        message
      } = _ref3;
      if (threadReplySelectType === _const.ThreadReplySelectType.THREAD) {
        onReplyInThread({
          message
        });
      } else if (threadReplySelectType === _const.ThreadReplySelectType.PARENT) {
        var _message$parentMessag5;
        scrollToMessage((_message$parentMessag5 = message.parentMessage) === null || _message$parentMessag5 === void 0 ? void 0 : _message$parentMessag5.createdAt, message.parentMessageId);
      }
    }
  }))), showMenu && ((message === null || message === void 0 ? void 0 : (_message$isUserMessag = message.isUserMessage) === null || _message$isUserMessag === void 0 ? void 0 : _message$isUserMessag.call(message)) || (message === null || message === void 0 ? void 0 : (_message$isFileMessag = message.isFileMessage) === null || _message$isFileMessag === void 0 ? void 0 : _message$isFileMessag.call(message))) && /*#__PURE__*/React__default["default"].createElement(index$2.MobileMenu, {
    parentRef: contentRef,
    channel: channel,
    hideMenu: () => {
      setShowMenu(false);
    },
    message: message,
    isReactionEnabled: isReactionEnabledInChannel,
    isByMe: isByMe,
    userId: userId,
    replyType: replyType,
    disabled: disabled,
    showRemove: showRemove,
    emojiContainer: emojiContainer,
    resendMessage: resendMessage,
    setQuoteMessage: setQuoteMessage,
    toggleReaction: toggleReaction,
    showEdit: showEdit,
    onReplyInThread: _ref4 => {
      let {
        message
      } = _ref4;
      if (threadReplySelectType === _const.ThreadReplySelectType.THREAD) {
        onReplyInThread === null || onReplyInThread === void 0 ? void 0 : onReplyInThread({
          message
        });
      } else if (threadReplySelectType === _const.ThreadReplySelectType.PARENT) {
        var _message$parentMessag6;
        scrollToMessage === null || scrollToMessage === void 0 ? void 0 : scrollToMessage((message === null || message === void 0 ? void 0 : (_message$parentMessag6 = message.parentMessage) === null || _message$parentMessag6 === void 0 ? void 0 : _message$parentMessag6.createdAt) || 0, (message === null || message === void 0 ? void 0 : message.parentMessageId) || 0);
      }
    }
  }));
}

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