'use strict';

var React = require('react');
var groupChannel = require('@sendbird/chat/groupChannel');
var useSendbirdStateContext = require('../../useSendbirdStateContext.js');
var ui_MessageInput = require('../../ui/MessageInput.js');
var _const = require('../../const-ab0f6056.js');
var Channel_components_SuggestedMentionList = require('../../Channel/components/SuggestedMentionList.js');
var Thread_context = require('../../ThreadProvider-572574d7.js');
var LocalizationContext = require('../../LocalizationContext-af96cf5f.js');
var VoiceMessageInputWrapper = require('../../VoiceMessageInputWrapper-db7f2b72.js');
var types = require('../../types-24accb8d.js');
require('../../withSendbird.js');
require('../../_rollupPluginBabelHelpers-c5955c81.js');
require('prop-types');
require('../../const-6691e574.js');
require('../../ui/IconButton.js');
require('../../ui/Button.js');
require('../../index-73643285.js');
require('../../stringSet-68748705.js');
require('dompurify');
require('../../consts-6ec77e9a.js');
require('../../ui/Icon.js');
require('../../index-f2146bae.js');
require('../../utils/message/getOutgoingMessageState.js');
require('../../consts-bc27beff.js');
require('../../consts-a422c2f9.js');
require('../../tokenize-d86ab0c7.js');
require('../../index-83d1d15e.js');
require('../../ui/Avatar.js');
require('../../ui/ImageRenderer.js');
require('../../uuid-0b4ed44c.js');
require('../../ChannelProvider-620bb354.js');
require('../../UserProfileContext-f068b0ba.js');
require('../../index-f65677b1.js');
require('../../topics-825e5d42.js');
require('../../compareIds-18b3b1d6.js');
require('@sendbird/chat/message');
require('../../ui/ContextMenu.js');
require('react-dom');
require('../../ui/SortByRow.js');
require('../../ui/ReactionButton.js');
require('../../consts-a5382bc1.js');
require('../context/types.js');
require('@sendbird/chat');
require('../../VoicePlayer/useVoicePlayer.js');
require('../../index-9d518988.js');
require('../../VoiceRecorder/context.js');
require('../../VoiceRecorder/useVoiceRecorder.js');
require('../../index-c1de8d8f.js');
require('../../ui/PlaybackTime.js');
require('../../ui/ProgressBar.js');
require('../../ui/TextButton.js');
require('../../color-afeb8dc2.js');
require('../../ui/Modal.js');
require('../../index-68c57136.js');
require('../../MediaQueryContext-8ec3f647.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

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

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

  // Reset when changing channel
  React.useEffect(() => {
    setShowVoiceMessageInput(false);
  }, [currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.url]);
  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(_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) !== types.Role.OPERATOR) {
    return null;
  }
  return /*#__PURE__*/React__default["default"].createElement("div", {
    className: `sendbird-thread-message-input${showVoiceMessageInput ? '--voice-message' : ''} ${className}`
  }, displaySuggestedMentionList && /*#__PURE__*/React__default["default"].createElement(Channel_components_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["default"].createElement(VoiceMessageInputWrapper.VoiceMessageInputWrapper, {
    channel: currentChannel,
    onSubmitClick: (recordedFile, duration) => {
      sendVoiceMessage(recordedFile, duration, parentMessage);
      setShowVoiceMessageInput(false);
    },
    onCancelClick: () => {
      setShowVoiceMessageInput(false);
    }
  }) : /*#__PURE__*/React__default["default"].createElement(ui_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) === types.Role.OPERATOR) && stringSet.MESSAGE_INPUT__PLACE_HOLDER__DISABLED || (currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.myMutedState) === groupChannel.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 === _const.MessageInputKeys.Enter && ableMention || e.key === _const.MessageInputKeys.ArrowUp || e.key === _const.MessageInputKeys.ArrowDown)) {
        setMessageInputEvent(e);
        return true;
      }
      return false;
    }
  }));
};
var ThreadMessageInput$1 = /*#__PURE__*/React__default["default"].forwardRef(ThreadMessageInput);

module.exports = ThreadMessageInput$1;
//# sourceMappingURL=ThreadMessageInput.js.map
