import React__default, { useRef, useState, useEffect } from 'react';
import { MutedState } from '@sendbird/chat/groupChannel';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import MessageInput from '../../ui/MessageInput.js';
import { M as MessageInputKeys } from '../../const-ddd9d461.js';
import SuggestedMentionList from '../../Channel/components/SuggestedMentionList.js';
import { u as useThreadContext } from '../../ThreadProvider-7f67575f.js';
import { u as useLocalization } from '../../LocalizationContext-426f3d58.js';
import { V as VoiceMessageInputWrapper } from '../../VoiceMessageInputWrapper-3adde78a.js';
import { R as Role } from '../../types-d72e53f1.js';
import { u as useDirtyGetMentions } from '../../useDirtyGetMentions-f302bddb.js';
import { a as isDisabledBecauseFrozen, b as isDisabledBecauseMuted } from '../../ChannelProvider-3bd92d16.js';
import '../../withSendbird.js';
import '../../_rollupPluginBabelHelpers-a69fd1ce.js';
import 'prop-types';
import '../../const-9982664a.js';
import '../../ui/IconButton.js';
import '../../ui/Button.js';
import '../../index-4105bc4d.js';
import '../../stringSet-a62108b2.js';
import 'dompurify';
import '../../consts-15e764c0.js';
import '../../ui/Icon.js';
import '../../index-f83f0aac.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../consts-f99cd0da.js';
import '../../consts-2eeaa3de.js';
import '../../tokenize-eec42500.js';
import '../../index.module-8bb7760e.js';
import '../../index-90420b99.js';
import '../../ui/Avatar.js';
import '../../ui/ImageRenderer.js';
import '../../uuid-2e8efc75.js';
import '../../UserProfileContext-a59f0533.js';
import '../../index-e7f4cbdf.js';
import '../../topics-247fcf3a.js';
import '../../compareIds-aca19765.js';
import '@sendbird/chat/message';
import '../../consts-e64103c6.js';
import '../../resolvedReplyType-7bde5f56.js';
import '../context/types.js';
import '@sendbird/chat';
import '../../VoicePlayer/useVoicePlayer.js';
import '../../index-52ba73b9.js';
import '../../VoiceRecorder/context.js';
import '../../ui/Modal.js';
import 'react-dom';
import '../../index-3e548496.js';
import '../../MediaQueryContext-b531adc9.js';
import '../../VoiceRecorder/useVoiceRecorder.js';
import '../../index-8567d9a9.js';
import '../../ui/PlaybackTime.js';
import '../../ui/ProgressBar.js';
import '../../ui/TextButton.js';
import '../../color-e708f53d.js';

const ThreadMessageInput = (props, ref) => {
  const {
    className,
    renderFileUploadIcon,
    renderVoiceMessageIcon,
    renderSendMessageIcon
  } = props;
  const propsDisabled = props.disabled;
  const {
    config
  } = useSendbirdStateContext();
  const {
    stringSet
  } = useLocalization();
  const {
    isMentionEnabled,
    isOnline,
    userMention,
    isVoiceMessageEnabled,
    logger
  } = config;
  const {
    currentChannel,
    parentMessage,
    sendMessage,
    sendFileMessage,
    sendVoiceMessage,
    isMuted,
    isChannelFrozen,
    allThreadMessages
  } = useThreadContext();
  const messageInputRef = useRef();
  const disabled = propsDisabled || isMuted || !((currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.myRole) === Role.OPERATOR) && isChannelFrozen || parentMessage === null;

  // mention
  const [mentionNickname, setMentionNickname] = useState('');
  const [mentionedUsers, setMentionedUsers] = useState([]);
  const [mentionedUserIds, setMentionedUserIds] = useState([]);
  const [selectedUser, setSelectedUser] = useState(null);
  const [mentionSuggestedUsers, setMentionSuggestedUsers] = useState([]);
  const [messageInputEvent, setMessageInputEvent] = useState(null);
  const [showVoiceMessageInput, setShowVoiceMessageInput] = useState(false);
  const displaySuggestedMentionList = isOnline && isMentionEnabled && mentionNickname.length > 0 && !isDisabledBecauseFrozen(currentChannel) && !isDisabledBecauseMuted(currentChannel) && !(currentChannel !== null && currentChannel !== void 0 && currentChannel.isBroadcast);

  // Reset when changing channel
  useEffect(() => {
    setShowVoiceMessageInput(false);
  }, [currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.url]);
  const mentionNodes = useDirtyGetMentions({
    ref: ref || messageInputRef
  }, {
    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(_ref => {
      let {
        userId
      } = _ref;
      const i = mentionedUserIds.indexOf(userId);
      if (i < 0) {
        return false;
      } else {
        mentionedUserIds.splice(i, 1);
        return true;
      }
    }));
  }, [mentionedUserIds]);
  if (currentChannel !== null && currentChannel !== void 0 && currentChannel.isBroadcast && (currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.myRole) !== Role.OPERATOR) {
    return null;
  }
  return /*#__PURE__*/React__default.createElement("div", {
    className: `sendbird-thread-message-input${showVoiceMessageInput ? '--voice-message' : ''} ${className}`
  }, displaySuggestedMentionList && /*#__PURE__*/React__default.createElement(SuggestedMentionList, {
    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
  }), showVoiceMessageInput ? /*#__PURE__*/React__default.createElement(VoiceMessageInputWrapper, {
    channel: currentChannel,
    onSubmitClick: (recordedFile, duration) => {
      sendVoiceMessage(recordedFile, duration, parentMessage);
      setShowVoiceMessageInput(false);
    },
    onCancelClick: () => {
      setShowVoiceMessageInput(false);
    }
  }) : /*#__PURE__*/React__default.createElement(MessageInput, {
    className: "sendbird-thread-message-input__message-input",
    messageFieldId: "sendbird-message-input-text-field--thread",
    disabled: disabled,
    channel: currentChannel,
    setMentionedUsers: setMentionedUsers,
    channelUrl: currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.url,
    mentionSelectedUser: selectedUser,
    isMentionEnabled: isMentionEnabled,
    isVoiceMessageEnabled: isVoiceMessageEnabled,
    onVoiceMessageIconClick: () => {
      setShowVoiceMessageInput(true);
    },
    renderFileUploadIcon: renderFileUploadIcon,
    renderVoiceMessageIcon: renderVoiceMessageIcon,
    renderSendMessageIcon: renderSendMessageIcon,
    ref: ref || messageInputRef,
    placeholder: (currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.isFrozen) && !((currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.myRole) === Role.OPERATOR) && stringSet.MESSAGE_INPUT__PLACE_HOLDER__DISABLED || (currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.myMutedState) === MutedState.MUTED && stringSet.MESSAGE_INPUT__PLACE_HOLDER__MUTED_SHORT || (allThreadMessages.length > 0 ? stringSet.THREAD__INPUT__REPLY_TO_THREAD : stringSet.THREAD__INPUT__REPLY_IN_THREAD),
    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);
    },
    onSendMessage: _ref2 => {
      var _currentChannel$endTy;
      let {
        message,
        mentionTemplate
      } = _ref2;
      sendMessage({
        message: message,
        mentionedUsers,
        mentionTemplate: mentionTemplate,
        quoteMessage: parentMessage
      });
      setMentionNickname('');
      setMentionedUsers([]);
      currentChannel === null || currentChannel === void 0 ? void 0 : (_currentChannel$endTy = currentChannel.endTyping) === null || _currentChannel$endTy === void 0 ? void 0 : _currentChannel$endTy.call(currentChannel);
    },
    onFileUpload: file => {
      sendFileMessage(file, parentMessage);
    },
    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;
    }
  }));
};
var ThreadMessageInput$1 = /*#__PURE__*/React__default.forwardRef(ThreadMessageInput);

export { ThreadMessageInput$1 as default };
//# sourceMappingURL=ThreadMessageInput.js.map
