'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-b19292fd.js');
var Channel_components_SuggestedMentionList = require('../../Channel/components/SuggestedMentionList.js');
var Thread_context = require('../../ThreadProvider-c83249cf.js');
var LocalizationContext = require('../../LocalizationContext-8cc9717d.js');
var VoiceMessageInputWrapper = require('../../VoiceMessageInputWrapper-b46d3514.js');
var types = require('../../types-9466af03.js');
var useDirtyGetMentions = require('../../useDirtyGetMentions-cc476482.js');
var Channel_context = require('../../ChannelProvider-39d1e9a4.js');
require('../../withSendbird.js');
require('../../_rollupPluginBabelHelpers-acf412a3.js');
require('prop-types');
require('../../const-a3e169f4.js');
require('../../ui/IconButton.js');
require('../../ui/Button.js');
require('../../index-35633fb6.js');
require('../../stringSet-ce782136.js');
require('dompurify');
require('../../consts-d04401ad.js');
require('../../ui/Icon.js');
require('../../index-a91b85d9.js');
require('../../utils/message/getOutgoingMessageState.js');
require('../../consts-016f07ac.js');
require('../../consts-eb163887.js');
require('../../tokenize-88b021e0.js');
require('../../index.module-e3392caa.js');
require('../../index-d749c4c2.js');
require('../../ui/Avatar.js');
require('../../ui/ImageRenderer.js');
require('../../uuid-1eceb0c8.js');
require('../../UserProfileContext-d993c96b.js');
require('../../index-3fc4f7d7.js');
require('../../topics-e24ef339.js');
require('../../compareIds-784d9a4e.js');
require('@sendbird/chat/message');
require('../../consts-9641990e.js');
require('../../resolvedReplyType-930f5b54.js');
require('../context/types.js');
require('@sendbird/chat');
require('../../VoicePlayer/useVoicePlayer.js');
require('../../index-90711a57.js');
require('../../VoiceRecorder/context.js');
require('../../ui/Modal.js');
require('react-dom');
require('../../index-8029e07a.js');
require('../../MediaQueryContext-fad211ad.js');
require('../../VoiceRecorder/useVoiceRecorder.js');
require('../../index-cb4e4067.js');
require('../../ui/PlaybackTime.js');
require('../../ui/ProgressBar.js');
require('../../ui/TextButton.js');
require('../../color-5cfb41c7.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,
    logger
  } = 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 [messageInputEvent, setMessageInputEvent] = React.useState(null);
  const [showVoiceMessageInput, setShowVoiceMessageInput] = React.useState(false);
  const displaySuggestedMentionList = isOnline && isMentionEnabled && mentionNickname.length > 0 && !Channel_context.isDisabledBecauseFrozen(currentChannel) && !Channel_context.isDisabledBecauseMuted(currentChannel) && !(currentChannel !== null && currentChannel !== void 0 && currentChannel.isBroadcast);

  // Reset when changing channel
  React.useEffect(() => {
    setShowVoiceMessageInput(false);
  }, [currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.url]);
  const mentionNodes = useDirtyGetMentions.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);
  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
