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-8daee9fc.js';
import SuggestedMentionList from '../../Channel/components/SuggestedMentionList.js';
import { u as useThreadContext } from '../../ThreadProvider-a3d3b241.js';
import { u as useLocalization } from '../../LocalizationContext-57b46f6c.js';
import { V as VoiceMessageInputWrapper } from '../../VoiceMessageInputWrapper-bdc53de4.js';
import { R as Role } from '../../types-bffbc5f5.js';
import { u as useDirtyGetMentions } from '../../useDirtyGetMentions-aac697f4.js';
import { a as isDisabledBecauseFrozen, b as isDisabledBecauseMuted } from '../../ChannelProvider-b41fde4c.js';
import '../../withSendbird.js';
import '../../_rollupPluginBabelHelpers-0bc76382.js';
import 'prop-types';
import '../../const-e51dda0c.js';
import '../../ui/IconButton.js';
import '../../ui/Button.js';
import '../../index-4023d1b2.js';
import '../../stringSet-7a8ce1ce.js';
import 'dompurify';
import '../../consts-d0b365df.js';
import '../../ui/Icon.js';
import '../../index-e2601165.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../consts-d0cfee49.js';
import '../../consts-7aab5aff.js';
import '../../tokenize-003bf3f7.js';
import '../../index-865112db.js';
import '../../ui/Avatar.js';
import '../../ui/ImageRenderer.js';
import '../../uuid-1f1bbd9c.js';
import '../../UserProfileContext-29074346.js';
import '../../index-f1701c11.js';
import '../../topics-5ff88d6f.js';
import '../../compareIds-bc881fe7.js';
import '@sendbird/chat/message';
import '../../consts-3c0ff1ed.js';
import '../context/types.js';
import '@sendbird/chat';
import '../../VoicePlayer/useVoicePlayer.js';
import '../../index-d847a382.js';
import '../../VoiceRecorder/context.js';
import '../../VoiceRecorder/useVoiceRecorder.js';
import '../../index-1e5cf284.js';
import '../../ui/PlaybackTime.js';
import '../../ui/ProgressBar.js';
import '../../ui/TextButton.js';
import '../../color-2ed716e6.js';
import '../../ui/Modal.js';
import 'react-dom';
import '../../index-7bff4ce0.js';
import '../../MediaQueryContext-fbe7af77.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
