'use strict';

var React = require('react');
var Channel_context = require('../../ChannelProvider-620bb354.js');
var ui_MessageInput = require('../../ui/MessageInput.js');
var ui_QuoteMessageInput = require('../../ui/QuoteMessageInput.js');
var LocalizationContext = require('../../LocalizationContext-af96cf5f.js');
var useSendbirdStateContext = require('../../useSendbirdStateContext.js');
var Channel_components_SuggestedMentionList = require('./SuggestedMentionList.js');
var _const = require('../../const-ab0f6056.js');
var VoiceMessageInputWrapper = require('../../VoiceMessageInputWrapper-db7f2b72.js');
require('../../UserProfileContext-f068b0ba.js');
require('prop-types');
require('../../index-f65677b1.js');
require('../../index-83d1d15e.js');
require('../../topics-825e5d42.js');
require('../../index-f2146bae.js');
require('../../_rollupPluginBabelHelpers-c5955c81.js');
require('../../utils/message/getOutgoingMessageState.js');
require('../../compareIds-18b3b1d6.js');
require('../../const-6691e574.js');
require('@sendbird/chat/groupChannel');
require('../../uuid-0b4ed44c.js');
require('@sendbird/chat/message');
require('../../ui/ContextMenu.js');
require('react-dom');
require('../../ui/SortByRow.js');
require('../../index-73643285.js');
require('../../stringSet-68748705.js');
require('../../ui/ReactionButton.js');
require('../../ui/ImageRenderer.js');
require('../../ui/Icon.js');
require('../../consts-a5382bc1.js');
require('../../ui/IconButton.js');
require('../../ui/Button.js');
require('dompurify');
require('../../consts-6ec77e9a.js');
require('../../consts-bc27beff.js');
require('../../consts-a422c2f9.js');
require('../../tokenize-d86ab0c7.js');
require('../../withSendbird.js');
require('../../ui/Avatar.js');
require('../../ThreadProvider-572574d7.js');
require('../../Thread/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 MessageInputWrapper = (props, ref) => {
  const {
    value,
    renderFileUploadIcon,
    renderVoiceMessageIcon,
    renderSendMessageIcon
  } = props;
  const propDisabled = props.disabled;
  const {
    currentGroupChannel,
    initialized,
    quoteMessage,
    sendMessage,
    sendFileMessage,
    sendVoiceMessage,
    setQuoteMessage,
    messageInputRef,
    renderUserMentionItem
  } = Channel_context.useChannelContext();
  const globalStore = useSendbirdStateContext();
  const channel = currentGroupChannel;
  const {
    isOnline,
    isMentionEnabled,
    userMention,
    isVoiceMessageEnabled
  } = globalStore === null || globalStore === void 0 ? void 0 : globalStore.config;
  const maxUserMentionCount = (userMention === null || userMention === void 0 ? void 0 : userMention.maxMentionCount) || 10;
  const maxUserSuggestionCount = (userMention === null || userMention === void 0 ? void 0 : userMention.maxSuggestionCount) || 15;
  const {
    stringSet
  } = React.useContext(LocalizationContext.LocalizationContext);
  const [mentionNickname, setMentionNickname] = React.useState('');
  // todo: set type
  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 disabled = propDisabled || !initialized || Channel_context.isDisabledBecauseFrozen(channel) || Channel_context.isDisabledBecauseMuted(channel) || !isOnline;
  const isOperator = Channel_context.isOperator(channel);
  const isBroadcast = channel === null || channel === void 0 ? void 0 : channel.isBroadcast;
  const displaySuggestedMentionList = isOnline && isMentionEnabled && mentionNickname.length > 0 && !Channel_context.isDisabledBecauseFrozen(channel) && !Channel_context.isDisabledBecauseMuted(channel);

  // Reset when channel changes
  React.useEffect(() => {
    setMentionNickname('');
    setMentionedUsers([]);
    setMentionedUserIds([]);
    setSelectedUser(null);
    setMentionSuggestedUsers([]);
    setAbleMention(true);
    setMessageInputEvent(null);
    setShowVoiceMessageInput(false);
  }, [channel === null || channel === void 0 ? void 0 : channel.url]);
  React.useEffect(() => {
    if ((mentionedUsers === null || mentionedUsers === void 0 ? void 0 : mentionedUsers.length) >= maxUserMentionCount) {
      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]);

  // broadcast channel + not operator
  if (isBroadcast && !isOperator) {
    return null;
  }
  // other conditions
  return /*#__PURE__*/React__default["default"].createElement("div", {
    className: `sendbird-message-input-wrapper${showVoiceMessageInput ? '--voice-message' : ''}`
  }, 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: maxUserMentionCount,
    maxSuggestionCount: maxUserSuggestionCount
  }), quoteMessage && /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-message-input-wrapper__quote-message-input"
  }, /*#__PURE__*/React__default["default"].createElement(ui_QuoteMessageInput, {
    replyingMessage: quoteMessage,
    onClose: () => setQuoteMessage(null)
  })), showVoiceMessageInput ? /*#__PURE__*/React__default["default"].createElement(VoiceMessageInputWrapper.VoiceMessageInputWrapper, {
    channel: channel,
    onSubmitClick: (recordedFile, duration) => {
      sendVoiceMessage(recordedFile, duration, quoteMessage);
      setQuoteMessage(null);
      setShowVoiceMessageInput(false);
    },
    onCancelClick: () => {
      setShowVoiceMessageInput(false);
    }
  }) : /*#__PURE__*/React__default["default"].createElement(ui_MessageInput, {
    className: "sendbird-message-input-wrapper__message-input",
    value: value,
    channelUrl: channel === null || channel === void 0 ? void 0 : channel.url,
    mentionSelectedUser: selectedUser,
    isMentionEnabled: isMentionEnabled,
    isVoiceMessageEnabled: isVoiceMessageEnabled,
    onVoiceMessageIconClick: () => {
      setShowVoiceMessageInput(true);
    },
    setMentionedUsers: setMentionedUsers,
    channel: channel,
    placeholder: quoteMessage && stringSet.MESSAGE_INPUT__QUOTE_REPLY__PLACE_HOLDER || Channel_context.isDisabledBecauseFrozen(channel) && stringSet.MESSAGE_INPUT__PLACE_HOLDER__DISABLED || Channel_context.isDisabledBecauseMuted(channel) && stringSet.MESSAGE_INPUT__PLACE_HOLDER__MUTED,
    ref: ref || messageInputRef,
    disabled: disabled,
    renderFileUploadIcon: renderFileUploadIcon,
    renderSendMessageIcon: renderSendMessageIcon,
    renderVoiceMessageIcon: renderVoiceMessageIcon,
    onStartTyping: () => {
      channel === null || channel === void 0 ? void 0 : channel.startTyping();
    },
    onSendMessage: _ref2 => {
      var _channel$endTyping;
      let {
        message,
        mentionTemplate
      } = _ref2;
      sendMessage({
        message,
        quoteMessage,
        mentionedUsers,
        mentionTemplate
      });
      setMentionNickname('');
      setMentionedUsers([]);
      setQuoteMessage(null);
      channel === null || channel === void 0 ? void 0 : (_channel$endTyping = channel.endTyping) === null || _channel$endTyping === void 0 ? void 0 : _channel$endTyping.call(channel);
    },
    onFileUpload: file => {
      sendFileMessage(file, quoteMessage);
      setQuoteMessage(null);
    },
    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 MessageInputWrapper$1 = /*#__PURE__*/React__default["default"].forwardRef(MessageInputWrapper);

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