'use strict';

var _rollupPluginBabelHelpers = require('../../_rollupPluginBabelHelpers-21ae158b.js');
var React = require('react');
var index$1 = require('../../index-892b4b7f.js');
var RemoveMessageModal = require('../../RemoveMessageModal-81daa8a0.js');
var Thread_components_ParentMessageInfoItem = require('./ParentMessageInfoItem.js');
var index = require('../../index-1b3c69da.js');
var Channel_context = require('../../ChannelProvider-a05d259f.js');
var LocalizationContext = require('../../LocalizationContext-44d2f61b.js');
var useSendbirdStateContext = require('../../useSendbirdStateContext.js');
var Thread_context = require('../../ThreadProvider-bac1bea8.js');
var UserProfileContext = require('../../UserProfileContext-9d843d68.js');
var Channel_components_SuggestedMentionList = require('../../Channel/components/SuggestedMentionList.js');
var ui_Avatar = require('../../ui/Avatar.js');
var ui_Label = require('../../index-2bfd366f.js');
var ui_FileViewer = require('../../ui/FileViewer.js');
var ui_MessageItemMenu = require('../../ui/MessageItemMenu.js');
var ui_MessageItemReactionMenu = require('../../ui/MessageItemReactionMenu.js');
var ui_ContextMenu = require('../../ui/ContextMenu.js');
var ui_UserProfile = require('../../ui/UserProfile.js');
var ui_MessageInput = require('../../ui/MessageInput.js');
var _const = require('../../const-bd02657f.js');
var types = require('../../types-df5bd4e5.js');
var MediaQueryContext = require('../../MediaQueryContext-5d311a86.js');
var useLongPress = require('../../useLongPress-83b6a2f1.js');
var index$2 = require('../../index-78a1037e.js');
var useDirtyGetMentions = require('../../useDirtyGetMentions-1500af53.js');
require('../../index-88e8fdfa.js');
require('../../ui/Modal.js');
require('react-dom');
require('../../index-0a446cca.js');
require('../../ui/Button.js');
require('prop-types');
require('../../stringSet-7aebf034.js');
require('../../ui/Icon.js');
require('../../ui/IconButton.js');
require('../../ui/ImageRenderer.js');
require('../../ui/TextButton.js');
require('../../color-0c04efbd.js');
require('../../ui/EmojiReactions.js');
require('../../ui/ReactionBadge.js');
require('../../ui/ReactionButton.js');
require('../../utils-6a748c02.js');
require('../../ui/BottomSheet.js');
require('../../ui/UserListItem.js');
require('../../ui/MutedAvatarOverlay.js');
require('../../ui/Checkbox.js');
require('../../uuid-dee74248.js');
require('../../sendbirdSelectors.js');
require('../../topics-373d2156.js');
require('../../withSendbird.js');
require('../../ui/SortByRow.js');
require('../../utils/message/getOutgoingMessageState.js');
require('../../ui/Tooltip.js');
require('../../ui/TooltipWrapper.js');
require('../../Message/context.js');
require('../../ui/VoiceMessageItemBody.js');
require('../../ui/ProgressBar.js');
require('../../VoicePlayer/useVoicePlayer.js');
require('../../index-50c4a83b.js');
require('../../consts-900e72b9.js');
require('../../VoiceRecorder/context.js');
require('../../ui/PlaybackTime.js');
require('../../ui/Loader.js');
require('../../index-8cd9b25d.js');
require('../../index.module-5a6a79b6.js');
require('../../tokenize-44139f1e.js');
require('../../ui/MentionLabel.js');
require('../../ui/LinkLabel.js');
require('../../const-1ea78727.js');
require('../../compareIds-a846891d.js');
require('@sendbird/chat/groupChannel');
require('@sendbird/chat/message');
require('../../resolvedReplyType-14fa0f85.js');
require('../context/types.js');
require('@sendbird/chat');
require('dompurify');
require('../../consts-867f1ae6.js');
require('../../consts-b93ef9ea.js');
require('../../consts-bce71b24.js');

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

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

function ParentMessageInfo(_ref) {
  var _stores$userStore$use, _stores$userStore$use2, _parentMessage$isUser, _currentChannel$membe3, _currentChannel$membe4, _parentMessage$sender4;
  let {
    className
  } = _ref;
  const {
    stores,
    config
  } = useSendbirdStateContext();
  const {
    isMentionEnabled,
    isReactionEnabled,
    replyType,
    isOnline,
    userMention,
    logger
  } = config;
  const userId = (_stores$userStore$use = (_stores$userStore$use2 = stores.userStore.user) === null || _stores$userStore$use2 === void 0 ? void 0 : _stores$userStore$use2.userId) !== null && _stores$userStore$use !== void 0 ? _stores$userStore$use : '';
  const {
    dateLocale
  } = LocalizationContext.useLocalization();
  const {
    currentChannel,
    parentMessage,
    allThreadMessages,
    emojiContainer,
    toggleReaction,
    updateMessage,
    deleteMessage,
    onMoveToParentMessage,
    onHeaderActionClick,
    isMuted,
    isChannelFrozen
  } = Thread_context.useThreadContext();
  const {
    isMobile
  } = MediaQueryContext.useMediaQueryContext();
  const [showRemove, setShowRemove] = React.useState(false);
  const [supposedHover, setSupposedHover] = React.useState(false);
  const [showFileViewer, setShowFileViewer] = React.useState(false);
  const usingReaction = Channel_context.getIsReactionEnabled({
    globalLevel: isReactionEnabled,
    isSuper: currentChannel.isSuper,
    isBroadcast: currentChannel.isBroadcast
  });
  const isByMe = userId === parentMessage.sender.userId;

  // Mobile
  const mobileMenuRef = React.useRef(null);
  const [showMobileMenu, setShowMobileMenu] = React.useState(false);
  const longPress = useLongPress.useLongPress({
    onLongPress: () => {
      if (isMobile) {
        setShowMobileMenu(true);
      }
    }
  }, {
    shouldPreventDefault: false
  });

  // Edit message
  const [showEditInput, setShowEditInput] = React.useState(false);
  const disabled = !isOnline || isMuted || isChannelFrozen && !((currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.myRole) === types.Role.OPERATOR);

  // Mention
  const editMessageInputRef = React.useRef(null);
  const [mentionNickname, setMentionNickname] = React.useState('');
  const [mentionedUsers, setMentionedUsers] = React.useState([]);
  const [mentionedUserIds, setMentionedUserIds] = React.useState([]);
  const [messageInputEvent, setMessageInputEvent] = React.useState(null);
  const [selectedUser, setSelectedUser] = React.useState(null);
  const [mentionSuggestedUsers, setMentionSuggestedUsers] = React.useState([]);
  const displaySuggestedMentionList = isOnline && isMentionEnabled && mentionNickname.length > 0 && !isMuted && !(isChannelFrozen && !(currentChannel.myRole === types.Role.OPERATOR));
  const mentionNodes = useDirtyGetMentions.useDirtyGetMentions({
    ref: editMessageInputRef
  }, {
    logger
  });
  const ableMention = (mentionNodes === null || mentionNodes === void 0 ? void 0 : mentionNodes.length) < (userMention === null || userMention === void 0 ? void 0 : userMention.maxMentionCount);
  React.useEffect(() => {
    setMentionedUsers(mentionedUsers.filter(_ref2 => {
      let {
        userId
      } = _ref2;
      const i = mentionedUserIds.indexOf(userId);
      if (i < 0) {
        return false;
      } else {
        mentionedUserIds.splice(i, 1);
        return true;
      }
    }));
  }, [mentionedUserIds]);

  // User Profile
  const avatarRef = React.useRef(null);
  const {
    disableUserProfile,
    renderUserProfile
  } = React.useContext(UserProfileContext.UserProfileContext);
  if (showEditInput && parentMessage !== null && parentMessage !== void 0 && (_parentMessage$isUser = parentMessage.isUserMessage) !== null && _parentMessage$isUser !== void 0 && _parentMessage$isUser.call(parentMessage)) {
    return /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null, displaySuggestedMentionList && /*#__PURE__*/React__default["default"].createElement(Channel_components_SuggestedMentionList, {
      className: "parent-message-info--suggested-mention-list",
      targetNickname: mentionNickname,
      inputEvent: messageInputEvent
      // renderUserMentionItem={renderUserMentionItem}
      ,
      onUserItemClick: user => {
        if (user) {
          setMentionedUsers([...mentionedUsers, user]);
        }
        setMentionNickname('');
        setSelectedUser(user);
        setMessageInputEvent(null);
      },
      onFocusItemChange: () => {
        setMessageInputEvent(null);
      },
      onFetchUsers: users => {
        setMentionSuggestedUsers(users);
      },
      ableAddMention: ableMention,
      maxMentionCount: userMention === null || userMention === void 0 ? void 0 : userMention.maxMentionCount,
      maxSuggestionCount: userMention === null || userMention === void 0 ? void 0 : userMention.maxSuggestionCount
    }), /*#__PURE__*/React__default["default"].createElement(ui_MessageInput, {
      channel: currentChannel,
      isEdit: true,
      disabled: disabled,
      ref: editMessageInputRef,
      mentionSelectedUser: selectedUser,
      isMentionEnabled: isMentionEnabled,
      message: parentMessage,
      onStartTyping: () => {
        var _currentChannel$start;
        currentChannel === null || currentChannel === void 0 ? void 0 : (_currentChannel$start = currentChannel.startTyping) === null || _currentChannel$start === void 0 ? void 0 : _currentChannel$start.call(currentChannel);
      },
      onUpdateMessage: _ref3 => {
        var _currentChannel$endTy;
        let {
          messageId,
          message,
          mentionTemplate
        } = _ref3;
        updateMessage({
          messageId,
          message,
          mentionedUsers,
          mentionTemplate
        });
        setShowEditInput(false);
        currentChannel === null || currentChannel === void 0 ? void 0 : (_currentChannel$endTy = currentChannel.endTyping) === null || _currentChannel$endTy === void 0 ? void 0 : _currentChannel$endTy.call(currentChannel);
      },
      onCancelEdit: () => {
        var _currentChannel$endTy2;
        setMentionNickname('');
        setMentionedUsers([]);
        setMentionedUserIds([]);
        setMentionSuggestedUsers([]);
        setShowEditInput(false);
        currentChannel === null || currentChannel === void 0 ? void 0 : (_currentChannel$endTy2 = currentChannel.endTyping) === null || _currentChannel$endTy2 === void 0 ? void 0 : _currentChannel$endTy2.call(currentChannel);
      },
      onUserMentioned: user => {
        if ((selectedUser === null || selectedUser === void 0 ? void 0 : selectedUser.userId) === (user === null || user === void 0 ? void 0 : user.userId)) {
          setSelectedUser(null);
          setMentionNickname('');
        }
      },
      onMentionStringChange: mentionText => {
        setMentionNickname(mentionText);
      },
      onMentionedUserIdsUpdated: userIds => {
        setMentionedUserIds(userIds);
      },
      onKeyDown: e => {
        if (displaySuggestedMentionList && (mentionSuggestedUsers === null || mentionSuggestedUsers === void 0 ? void 0 : mentionSuggestedUsers.length) > 0 && (e.key === _const.MessageInputKeys.Enter && ableMention || e.key === _const.MessageInputKeys.ArrowUp || e.key === _const.MessageInputKeys.ArrowDown)) {
          setMessageInputEvent(e);
          return true;
        }
        return false;
      }
    }));
  }
  return /*#__PURE__*/React__default["default"].createElement("div", _rollupPluginBabelHelpers._extends({
    className: `sendbird-parent-message-info ${className}`
  }, isMobile ? _rollupPluginBabelHelpers._objectSpread2({}, longPress) : {}, {
    ref: mobileMenuRef
  }), /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu["default"], {
    menuTrigger: toggleDropdown => {
      var _currentChannel$membe, _currentChannel$membe2, _parentMessage$sender2;
      return /*#__PURE__*/React__default["default"].createElement(ui_Avatar["default"], {
        className: "sendbird-parent-message-info__sender",
        ref: avatarRef,
        src: (currentChannel === null || currentChannel === void 0 ? void 0 : (_currentChannel$membe = currentChannel.members) === null || _currentChannel$membe === void 0 ? void 0 : (_currentChannel$membe2 = _currentChannel$membe.find(m => {
          var _parentMessage$sender;
          return (m === null || m === void 0 ? void 0 : m.userId) === (parentMessage === null || parentMessage === void 0 ? void 0 : (_parentMessage$sender = parentMessage.sender) === null || _parentMessage$sender === void 0 ? void 0 : _parentMessage$sender.userId);
        })) === null || _currentChannel$membe2 === void 0 ? void 0 : _currentChannel$membe2.profileUrl) || (parentMessage === null || parentMessage === void 0 ? void 0 : (_parentMessage$sender2 = parentMessage.sender) === null || _parentMessage$sender2 === void 0 ? void 0 : _parentMessage$sender2.profileUrl),
        alt: "thread message sender",
        width: "40px",
        height: "40px",
        onClick: () => {
          if (!disableUserProfile) {
            toggleDropdown();
          }
        }
      });
    },
    menuItems: closeDropdown => /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.MenuItems, {
      parentRef: avatarRef,
      parentContainRef: avatarRef,
      closeDropdown: closeDropdown,
      style: {
        paddingTop: '0px',
        paddingBottom: '0px'
      }
    }, renderUserProfile ? renderUserProfile({
      user: parentMessage === null || parentMessage === void 0 ? void 0 : parentMessage.sender,
      close: closeDropdown
    }) : /*#__PURE__*/React__default["default"].createElement(ui_UserProfile, {
      user: parentMessage === null || parentMessage === void 0 ? void 0 : parentMessage.sender,
      currentUserId: userId,
      onSuccess: closeDropdown
    }))
  }), /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-parent-message-info__content"
  }, /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-parent-message-info__content__info"
  }, /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    className: `sendbird-parent-message-info__content__info__sender-name${usingReaction ? '--use-reaction' : ''}`,
    type: ui_Label.LabelTypography.CAPTION_2,
    color: ui_Label.LabelColors.ONBACKGROUND_2
  }, (currentChannel === null || currentChannel === void 0 ? void 0 : (_currentChannel$membe3 = currentChannel.members) === null || _currentChannel$membe3 === void 0 ? void 0 : (_currentChannel$membe4 = _currentChannel$membe3.find(member => {
    var _parentMessage$sender3;
    return (member === null || member === void 0 ? void 0 : member.userId) === (parentMessage === null || parentMessage === void 0 ? void 0 : (_parentMessage$sender3 = parentMessage.sender) === null || _parentMessage$sender3 === void 0 ? void 0 : _parentMessage$sender3.userId);
  })) === null || _currentChannel$membe4 === void 0 ? void 0 : _currentChannel$membe4.nickname) || (index.getSenderName === null || index.getSenderName === void 0 ? void 0 : index.getSenderName(parentMessage))), /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    className: "sendbird-parent-message-info__content__info__sent-at",
    type: ui_Label.LabelTypography.CAPTION_3,
    color: ui_Label.LabelColors.ONBACKGROUND_2
  }, index$1.format((parentMessage === null || parentMessage === void 0 ? void 0 : parentMessage.createdAt) || 0, 'p', {
    locale: dateLocale
  }))), /*#__PURE__*/React__default["default"].createElement(Thread_components_ParentMessageInfoItem, {
    message: parentMessage,
    showFileViewer: setShowFileViewer
  })), !isMobile && /*#__PURE__*/React__default["default"].createElement(ui_MessageItemMenu, {
    className: `sendbird-parent-message-info__context-menu ${usingReaction ? 'use-reaction' : ''} ${supposedHover ? 'sendbird-mouse-hover' : ''}`,
    channel: currentChannel,
    message: parentMessage,
    isByMe: userId === (parentMessage === null || parentMessage === void 0 ? void 0 : (_parentMessage$sender4 = parentMessage.sender) === null || _parentMessage$sender4 === void 0 ? void 0 : _parentMessage$sender4.userId),
    disableDeleteMessage: allThreadMessages.length > 0,
    replyType: replyType,
    showEdit: setShowEditInput,
    showRemove: setShowRemove,
    setSupposedHover: setSupposedHover,
    onMoveToParentMessage: () => {
      onMoveToParentMessage({
        message: parentMessage,
        channel: currentChannel
      });
    }
  }), usingReaction && !isMobile && /*#__PURE__*/React__default["default"].createElement(ui_MessageItemReactionMenu, {
    className: `sendbird-parent-message-info__reaction-menu ${supposedHover ? 'sendbird-mouse-hover' : ''}`,
    message: parentMessage,
    userId: userId,
    emojiContainer: emojiContainer,
    toggleReaction: toggleReaction,
    setSupposedHover: setSupposedHover
  }), showRemove && /*#__PURE__*/React__default["default"].createElement(RemoveMessageModal.RemoveMessage, {
    onCancel: () => setShowRemove(false),
    onSubmit: () => {
      onHeaderActionClick === null || onHeaderActionClick === void 0 ? void 0 : onHeaderActionClick();
    },
    message: parentMessage
  }), showFileViewer && /*#__PURE__*/React__default["default"].createElement(ui_FileViewer["default"], {
    message: parentMessage,
    onClose: () => setShowFileViewer(false),
    onDelete: () => {
      deleteMessage(parentMessage).then(() => {
        setShowFileViewer(false);
      });
    }
  }), showMobileMenu && /*#__PURE__*/React__default["default"].createElement(index$2.MobileMenu, {
    parentRef: mobileMenuRef,
    channel: currentChannel,
    message: parentMessage,
    userId: userId,
    replyType: replyType,
    hideMenu: () => {
      setShowMobileMenu(false);
    },
    deleteMenuState: (allThreadMessages === null || allThreadMessages === void 0 ? void 0 : allThreadMessages.length) === 0 ? 'ACTIVE' : 'HIDE',
    isReactionEnabled: usingReaction,
    isByMe: isByMe,
    emojiContainer: emojiContainer,
    showEdit: setShowEditInput,
    showRemove: setShowRemove,
    toggleReaction: toggleReaction,
    isOpenedFromThread: true
  }));
}

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