import { b as _extends, _ as _objectSpread2 } from '../../_rollupPluginBabelHelpers-a69fd1ce.js';
import React__default, { useState, useRef, useEffect, useContext } from 'react';
import { f as format } from '../../index-e7f4cbdf.js';
import { R as RemoveMessage } from '../../RemoveMessageModal-3d9ef84c.js';
import ParentMessageInfoItem from './ParentMessageInfoItem.js';
import { u as getSenderName } from '../../index-f83f0aac.js';
import { g as getIsReactionEnabled } from '../../ChannelProvider-3bd92d16.js';
import { u as useLocalization } from '../../LocalizationContext-426f3d58.js';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import { u as useThreadContext } from '../../ThreadProvider-7f67575f.js';
import { a as UserProfileContext } from '../../UserProfileContext-a59f0533.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-4105bc4d.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-ddd9d461.js';
import { R as Role } from '../../types-d72e53f1.js';
import { u as useMediaQueryContext } from '../../MediaQueryContext-b531adc9.js';
import { u as useLongPress } from '../../useLongPress-c0ba590c.js';
import { M as MobileMenu } from '../../index-eefc5b10.js';
import { u as useDirtyGetMentions } from '../../useDirtyGetMentions-f302bddb.js';
import '../../index-90420b99.js';
import '../../ui/Modal.js';
import 'react-dom';
import '../../index-3e548496.js';
import '../../ui/Button.js';
import 'prop-types';
import '../../stringSet-a62108b2.js';
import '../../ui/Icon.js';
import '../../ui/IconButton.js';
import '../../ui/ImageRenderer.js';
import '../../ui/TextButton.js';
import '../../color-e708f53d.js';
import '../../ui/EmojiReactions.js';
import '../../ui/ReactionBadge.js';
import '../../ui/ReactionButton.js';
import '../../utils-4bf6e4e6.js';
import '../../ui/BottomSheet.js';
import '../../ui/UserListItem.js';
import '../../ui/MutedAvatarOverlay.js';
import '../../ui/Checkbox.js';
import '../../uuid-2e8efc75.js';
import '../../sendbirdSelectors.js';
import '../../topics-247fcf3a.js';
import '../../withSendbird.js';
import '../../ui/SortByRow.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../ui/Tooltip.js';
import '../../ui/TooltipWrapper.js';
import '../../Message/context.js';
import '../../ui/VoiceMessageItemBody.js';
import '../../ui/ProgressBar.js';
import '../../VoicePlayer/useVoicePlayer.js';
import '../../index-52ba73b9.js';
import '../../consts-e64103c6.js';
import '../../VoiceRecorder/context.js';
import '../../ui/PlaybackTime.js';
import '../../ui/Loader.js';
import '../../index-89c84c27.js';
import '../../index.module-8bb7760e.js';
import '../../tokenize-eec42500.js';
import '../../ui/MentionLabel.js';
import '../../ui/LinkLabel.js';
import '../../const-9982664a.js';
import '../../compareIds-aca19765.js';
import '@sendbird/chat/groupChannel';
import '@sendbird/chat/message';
import '../../resolvedReplyType-7bde5f56.js';
import '../context/types.js';
import '@sendbird/chat';
import 'dompurify';
import '../../consts-15e764c0.js';
import '../../consts-f99cd0da.js';
import '../../consts-2eeaa3de.js';

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
  } = useLocalization();
  const {
    currentChannel,
    parentMessage,
    allThreadMessages,
    emojiContainer,
    toggleReaction,
    updateMessage,
    deleteMessage,
    onMoveToParentMessage,
    onHeaderActionClick,
    isMuted,
    isChannelFrozen
  } = useThreadContext();
  const {
    isMobile
  } = useMediaQueryContext();
  const [showRemove, setShowRemove] = useState(false);
  const [supposedHover, setSupposedHover] = useState(false);
  const [showFileViewer, setShowFileViewer] = useState(false);
  const usingReaction = getIsReactionEnabled({
    globalLevel: isReactionEnabled,
    isSuper: currentChannel.isSuper,
    isBroadcast: currentChannel.isBroadcast
  });
  const isByMe = userId === parentMessage.sender.userId;

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

  // 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 displaySuggestedMentionList = isOnline && isMentionEnabled && mentionNickname.length > 0 && !isMuted && !(isChannelFrozen && !(currentChannel.myRole === Role.OPERATOR));
  const mentionNodes = useDirtyGetMentions({
    ref: editMessageInputRef
  }, {
    logger
  });
  const ableMention = (mentionNodes === null || mentionNodes === void 0 ? void 0 : mentionNodes.length) < (userMention === null || userMention === void 0 ? void 0 : userMention.maxMentionCount);
  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, {
      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 === MessageInputKeys.Enter && ableMention || e.key === MessageInputKeys.ArrowUp || e.key === MessageInputKeys.ArrowDown)) {
          setMessageInputEvent(e);
          return true;
        }
        return false;
      }
    }));
  }
  return /*#__PURE__*/React__default.createElement("div", _extends({
    className: `sendbird-parent-message-info ${className}`
  }, isMobile ? _objectSpread2({}, longPress) : {}, {
    ref: mobileMenuRef
  }), /*#__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
  })), !isMobile && /*#__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 && !isMobile && /*#__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);
      });
    }
  }), showMobileMenu && /*#__PURE__*/React__default.createElement(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
  }));
}

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