import React__default, { useState, useRef, useEffect, useContext } from 'react';
import { f as format } from '../../index-f48cfdf7.js';
import { R as RemoveMessage } from '../../RemoveMessageModal-99b5bab4.js';
import ParentMessageInfoItem from './ParentMessageInfoItem.js';
import { A as getSenderName } from '../../index-dbe0e004.js';
import { u as useLocalization } from '../../LocalizationContext-9c54e783.js';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import { u as useThreadContext } from '../../ThreadProvider-0a3c5f37.js';
import { a as UserProfileContext } from '../../UserProfileContext-a8aa4c0f.js';
import SuggestedMentionList from '../../Channel/components/SuggestedMentionList.js';
import Avatar from '../../ui/Avatar.js';
import { L as Label, a as LabelTypography, b as LabelColors } from '../../index-4111594a.js';
import FileViewer from '../../ui/FileViewer.js';
import MessageItemMenu from '../../ui/MessageItemMenu.js';
import MessageItemReactionMenu from '../../ui/MessageItemReactionMenu.js';
import ContextMenu, { MenuItems } from '../../ui/ContextMenu.js';
import UserProfile from '../../ui/UserProfile.js';
import MessageInput from '../../ui/MessageInput.js';
import { M as MessageInputKeys } from '../../const-6a07b718.js';
import { R as Role } from '../../types-a85de720.js';
import '../../index-a9ddd0f5.js';
import '../../ui/Modal.js';
import 'react-dom';
import '../../index-2a938266.js';
import '../../ui/Button.js';
import 'prop-types';
import '../../stringSet-1f18348a.js';
import '../../ui/Icon.js';
import '../../ui/IconButton.js';
import '../../_rollupPluginBabelHelpers-b2d9bfc5.js';
import '../../MediaQueryContext-1988a4bc.js';
import '../../ui/ImageRenderer.js';
import '../../ui/TextButton.js';
import '../../color-3bac434a.js';
import '../../ui/EmojiReactions.js';
import '../../ui/Tooltip.js';
import '../../ui/TooltipWrapper.js';
import '../../ui/ReactionBadge.js';
import '../../ui/ReactionButton.js';
import '../../ui/VoiceMessageItemBody.js';
import '../../ui/ProgressBar.js';
import '../../VoicePlayer/useVoicePlayer.js';
import '../../index-38a78d99.js';
import '../../consts-c9746f2b.js';
import '../../VoiceRecorder/context.js';
import '../../withSendbird.js';
import '../../ui/PlaybackTime.js';
import '../../ui/Loader.js';
import '../../index-8df6bff0.js';
import '../../index.module-ab8289c1.js';
import '../../tokenize-63d895d3.js';
import '../../Message/context.js';
import '../../ui/MentionLabel.js';
import '../../ui/LinkLabel.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../context/types.js';
import '@sendbird/chat';
import '../../topics-d8d10f83.js';
import '@sendbird/chat/groupChannel';
import '../../uuid-48baadaf.js';
import '@sendbird/chat/message';
import '../../ChannelProvider-4ab3a851.js';
import '../../compareIds-1e0e668f.js';
import '../../const-5f722b91.js';
import '../../ui/SortByRow.js';
import '../../sendbirdSelectors.js';
import '../../utils-0c09801e.js';
import 'dompurify';
import '../../consts-d4c78cc0.js';
import '../../consts-aadaccca.js';
import '../../consts-790ae3a9.js';

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
  } = useLocalization === null || useLocalization === void 0 ? void 0 : useLocalization();
  const {
    currentChannel,
    parentMessage,
    allThreadMessages,
    emojiContainer,
    toggleReaction,
    updateMessage,
    deleteMessage,
    onMoveToParentMessage,
    onHeaderActionClick,
    isMuted,
    isChannelFrozen
  } = useThreadContext();
  const [showRemove, setShowRemove] = useState(false);
  const [supposedHover, setSupposedHover] = useState(false);
  const [showFileViewer, setShowFileViewer] = useState(false);
  const usingReaction = isReactionEnabled && !(currentChannel !== null && currentChannel !== void 0 && currentChannel.isSuper) && !(currentChannel !== null && currentChannel !== void 0 && currentChannel.isBroadcast);

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

  // Mention
  const editMessageInputRef = useRef(null);
  const [mentionNickname, setMentionNickname] = useState('');
  const [mentionedUsers, setMentionedUsers] = useState([]);
  const [mentionedUserIds, setMentionedUserIds] = useState([]);
  const [messageInputEvent, setMessageInputEvent] = useState(null);
  const [selectedUser, setSelectedUser] = useState(null);
  const [mentionSuggestedUsers, setMentionSuggestedUsers] = useState([]);
  const [ableMention, setAbleMention] = useState(true);
  const displaySuggestedMentionList = isOnline && isMentionEnabled && mentionNickname.length > 0 && !isMuted && !(isChannelFrozen && !(currentChannel.myRole === Role.OPERATOR));
  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]);
  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 = useRef(null);
  const {
    disableUserProfile,
    renderUserProfile
  } = useContext(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.createElement(React__default.Fragment, null, displaySuggestedMentionList && /*#__PURE__*/React__default.createElement(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.createElement(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 === MessageInputKeys.Enter && ableMention || e.key === MessageInputKeys.ArrowUp || e.key === MessageInputKeys.ArrowDown)) {
          setMessageInputEvent(e);
          return true;
        }
        return false;
      }
    }));
  }
  return /*#__PURE__*/React__default.createElement("div", {
    className: `sendbird-parent-message-info ${className}`
  }, /*#__PURE__*/React__default.createElement(ContextMenu, {
    menuTrigger: toggleDropdown => {
      var _currentChannel$membe, _currentChannel$membe2, _parentMessage$sender2;
      return /*#__PURE__*/React__default.createElement(Avatar, {
        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.createElement(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.createElement(UserProfile, {
      user: parentMessage === null || parentMessage === void 0 ? void 0 : parentMessage.sender,
      currentUserId: userId,
      onSuccess: closeDropdown
    }))
  }), /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-parent-message-info__content"
  }, /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-parent-message-info__content__info"
  }, /*#__PURE__*/React__default.createElement(Label, {
    className: `sendbird-parent-message-info__content__info__sender-name${usingReaction ? '--use-reaction' : ''}`,
    type: LabelTypography.CAPTION_2,
    color: 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) || (getSenderName === null || getSenderName === void 0 ? void 0 : getSenderName(parentMessage))), /*#__PURE__*/React__default.createElement(Label, {
    className: "sendbird-parent-message-info__content__info__sent-at",
    type: LabelTypography.CAPTION_3,
    color: LabelColors.ONBACKGROUND_2
  }, format((parentMessage === null || parentMessage === void 0 ? void 0 : parentMessage.createdAt) || 0, 'p', {
    locale: dateLocale
  }))), /*#__PURE__*/React__default.createElement(ParentMessageInfoItem, {
    message: parentMessage,
    showFileViewer: setShowFileViewer
  })), /*#__PURE__*/React__default.createElement(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.createElement(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.createElement(RemoveMessage, {
    onCancel: () => setShowRemove(false),
    onSubmit: () => {
      onHeaderActionClick === null || onHeaderActionClick === void 0 ? void 0 : onHeaderActionClick();
    },
    message: parentMessage
  }), showFileViewer && /*#__PURE__*/React__default.createElement(FileViewer, {
    message: parentMessage,
    onClose: () => setShowFileViewer(false),
    onDelete: () => {
      deleteMessage(parentMessage).then(() => {
        setShowFileViewer(false);
      });
    }
  }));
}

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