'use strict';

var React = require('react');
var index$1 = require('../../index-f65677b1.js');
var RemoveMessageModal = require('../../RemoveMessageModal-15d1dcbd.js');
var Thread_components_ParentMessageInfoItem = require('./ParentMessageInfoItem.js');
var index = require('../../index-f2146bae.js');
var LocalizationContext = require('../../LocalizationContext-af96cf5f.js');
var useSendbirdStateContext = require('../../useSendbirdStateContext.js');
var Thread_context = require('../../ThreadProvider-572574d7.js');
var UserProfileContext = require('../../UserProfileContext-f068b0ba.js');
var Channel_components_SuggestedMentionList = require('../../Channel/components/SuggestedMentionList.js');
var ui_Avatar = require('../../ui/Avatar.js');
var ui_Label = require('../../index-73643285.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-ab0f6056.js');
var types = require('../../types-24accb8d.js');
require('../../index-83d1d15e.js');
require('../../ui/Modal.js');
require('react-dom');
require('../../index-68c57136.js');
require('../../ui/Button.js');
require('prop-types');
require('../../stringSet-68748705.js');
require('../../ui/Icon.js');
require('../../ui/IconButton.js');
require('../../_rollupPluginBabelHelpers-c5955c81.js');
require('../../MediaQueryContext-8ec3f647.js');
require('../../ui/ImageRenderer.js');
require('../../ui/TextButton.js');
require('../../color-afeb8dc2.js');
require('../../ui/EmojiReactions.js');
require('../../ui/Tooltip.js');
require('../../ui/TooltipWrapper.js');
require('../../ui/ReactionBadge.js');
require('../../ui/ReactionButton.js');
require('../../ui/VoiceMessageItemBody.js');
require('../../ui/ProgressBar.js');
require('../../VoicePlayer/useVoicePlayer.js');
require('../../index-9d518988.js');
require('../../consts-a5382bc1.js');
require('../../VoiceRecorder/context.js');
require('../../withSendbird.js');
require('../../ui/PlaybackTime.js');
require('../../ui/Loader.js');
require('../../index-0b344bd7.js');
require('../../index.module-9f5edc7b.js');
require('../../tokenize-d86ab0c7.js');
require('../../Message/context.js');
require('../../ui/MentionLabel.js');
require('../../ui/LinkLabel.js');
require('../../utils/message/getOutgoingMessageState.js');
require('../context/types.js');
require('@sendbird/chat');
require('../../topics-825e5d42.js');
require('@sendbird/chat/groupChannel');
require('../../uuid-0b4ed44c.js');
require('@sendbird/chat/message');
require('../../ChannelProvider-620bb354.js');
require('../../compareIds-18b3b1d6.js');
require('../../const-6691e574.js');
require('../../ui/SortByRow.js');
require('../../sendbirdSelectors.js');
require('../../utils-d5176360.js');
require('dompurify');
require('../../consts-6ec77e9a.js');
require('../../consts-bc27beff.js');
require('../../consts-a422c2f9.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, _stores$userStore$use, _parentMessage$isUser, _currentChannel$membe3, _currentChannel$membe4, _parentMessage$sender4;
  let {
    className
  } = _ref;
  const {
    stores,
    config
  } = useSendbirdStateContext();
  const {
    isMentionEnabled,
    isReactionEnabled,
    replyType,
    isOnline,
    userMention
  } = config;
  const userId = stores === null || stores === void 0 ? void 0 : (_stores$userStore = stores.userStore) === null || _stores$userStore === void 0 ? void 0 : (_stores$userStore$use = _stores$userStore.user) === null || _stores$userStore$use === void 0 ? void 0 : _stores$userStore$use.userId;
  const {
    dateLocale
  } = LocalizationContext.useLocalization === null || LocalizationContext.useLocalization === void 0 ? void 0 : LocalizationContext.useLocalization();
  const {
    currentChannel,
    parentMessage,
    allThreadMessages,
    emojiContainer,
    toggleReaction,
    updateMessage,
    deleteMessage,
    onMoveToParentMessage,
    onHeaderActionClick,
    isMuted,
    isChannelFrozen
  } = Thread_context.useThreadContext();
  const [showRemove, setShowRemove] = React.useState(false);
  const [supposedHover, setSupposedHover] = React.useState(false);
  const [showFileViewer, setShowFileViewer] = React.useState(false);
  const usingReaction = isReactionEnabled && !(currentChannel !== null && currentChannel !== void 0 && currentChannel.isSuper) && !(currentChannel !== null && currentChannel !== void 0 && currentChannel.isBroadcast);

  // 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 [ableMention, setAbleMention] = React.useState(true);
  const displaySuggestedMentionList = isOnline && isMentionEnabled && mentionNickname.length > 0 && !isMuted && !(isChannelFrozen && !(currentChannel.myRole === types.Role.OPERATOR));
  React.useEffect(() => {
    if ((mentionedUsers === null || mentionedUsers === void 0 ? void 0 : mentionedUsers.length) >= (userMention === null || userMention === void 0 ? void 0 : userMention.maxMentionCount)) {
      setAbleMention(false);
    } else {
      setAbleMention(true);
    }
  }, [mentionedUsers]);
  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, {
      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", {
    className: `sendbird-parent-message-info ${className}`
  }, /*#__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
  })), /*#__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 && /*#__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);
      });
    }
  }));
}

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