'use strict';

var tslib_es6 = require('../../tslib.es6-d2ba1a6b.js');
var React = require('react');
var Channel_context = require('../../ChannelProvider-b1290e55.js');
var ui_MessageInput = require('../../ui/MessageInput.js');
var ui_QuoteMessageInput = require('../../ui/QuoteMessageInput.js');
var LocalizationContext = require('../../LocalizationContext-1f669b3c.js');
var useSendbirdStateContext = require('../../useSendbirdStateContext.js');
var Channel_components_SuggestedMentionList = require('./SuggestedMentionList.js');
var _const = require('../../const-fe838b59.js');
var VoiceMessageInputWrapper = require('../../VoiceMessageInputWrapper-5bb0ed00.js');
require('../../UserProfileContext-cde95db8.js');
require('prop-types');
require('../../index-81e19961.js');
require('../../index-f5502150.js');
require('../../topics-5edbb1b2.js');
require('../../index-b32fce67.js');
require('../../utils/message/getOutgoingMessageState.js');
require('../../_rollupPluginBabelHelpers-32557b21.js');
require('../../compareIds-32f97a73.js');
require('../../const-77194948.js');
require('@sendbird/chat/groupChannel');
require('../../uuid-28e4a814.js');
require('@sendbird/chat/message');
require('../../ui/ContextMenu.js');
require('react-dom');
require('../../ui/SortByRow.js');
require('../../index-1b11bb85.js');
require('../../stringSet-ddd351f0.js');
require('../../ui/ReactionButton.js');
require('../../ui/ImageRenderer.js');
require('../../ui/Icon.js');
require('../../consts-f82aaae9.js');
require('react-dom/server');
require('../../ui/IconButton.js');
require('../../ui/Button.js');
require('../../ui/MentionUserLabel.js');
require('dompurify');
require('../../withSendbird.js');
require('../../ui/Avatar.js');
require('../../ThreadProvider-5eafb9ef.js');
require('../../Thread/context/types.js');
require('@sendbird/chat');
require('../../VoicePlayer/useVoicePlayer.js');
require('../../index-26ff28a8.js');
require('../../VoiceRecorder/context.js');
require('../../lame.all.js');
require('../../VoiceRecorder/useVoiceRecorder.js');
require('../../index-2c7774d0.js');
require('../../ui/PlaybackTime.js');
require('../../ui/ProgressBar.js');
require('../../ui/TextButton.js');
require('../../color-97cfc61b.js');
require('../../ui/Modal.js');
require('../../index-577c9a9b.js');
require('../../MediaQueryContext-130143d7.js');

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

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

var MessageInputWrapper = function (props, ref) {
  var value = props.value,
      renderFileUploadIcon = props.renderFileUploadIcon,
      renderVoiceMessageIcon = props.renderVoiceMessageIcon,
      renderSendMessageIcon = props.renderSendMessageIcon;

  var _a = Channel_context.useChannelContext(),
      currentGroupChannel = _a.currentGroupChannel,
      initialized = _a.initialized,
      quoteMessage = _a.quoteMessage,
      sendMessage = _a.sendMessage,
      sendFileMessage = _a.sendFileMessage,
      sendVoiceMessage = _a.sendVoiceMessage,
      setQuoteMessage = _a.setQuoteMessage,
      messageInputRef = _a.messageInputRef,
      renderUserMentionItem = _a.renderUserMentionItem;

  var globalStore = useSendbirdStateContext();
  var channel = currentGroupChannel;

  var _b = globalStore === null || globalStore === void 0 ? void 0 : globalStore.config,
      isOnline = _b.isOnline,
      isMentionEnabled = _b.isMentionEnabled,
      userMention = _b.userMention,
      isVoiceMessageEnabled = _b.isVoiceMessageEnabled;

  var maxUserMentionCount = (userMention === null || userMention === void 0 ? void 0 : userMention.maxMentionCount) || 10;
  var maxUserSuggestionCount = (userMention === null || userMention === void 0 ? void 0 : userMention.maxSuggestionCount) || 15;
  var stringSet = React.useContext(LocalizationContext.LocalizationContext).stringSet;

  var _c = React.useState(''),
      mentionNickname = _c[0],
      setMentionNickname = _c[1]; // todo: set type


  var _d = React.useState([]),
      mentionedUsers = _d[0],
      setMentionedUsers = _d[1];

  var _e = React.useState([]),
      mentionedUserIds = _e[0],
      setMentionedUserIds = _e[1];

  var _f = React.useState(null),
      selectedUser = _f[0],
      setSelectedUser = _f[1];

  var _g = React.useState([]),
      mentionSuggestedUsers = _g[0],
      setMentionSuggestedUsers = _g[1];

  var _h = React.useState(true),
      ableMention = _h[0],
      setAbleMention = _h[1];

  var _j = React.useState(null),
      messageInputEvent = _j[0],
      setMessageInputEvent = _j[1];

  var _k = React.useState(false),
      showVoiceMessageInput = _k[0],
      setShowVoiceMessageInput = _k[1];

  var disabled = !initialized || Channel_context.isDisabledBecauseFrozen(channel) || Channel_context.isDisabledBecauseMuted(channel) || !isOnline;
  var isOperator = Channel_context.isOperator(channel);
  var isBroadcast = channel === null || channel === void 0 ? void 0 : channel.isBroadcast;
  var displaySuggestedMentionList = isOnline && isMentionEnabled && mentionNickname.length > 0 && !Channel_context.isDisabledBecauseFrozen(channel) && !Channel_context.isDisabledBecauseMuted(channel); // Reset when channel changes

  React.useEffect(function () {
    setMentionNickname('');
    setMentionedUsers([]);
    setMentionedUserIds([]);
    setSelectedUser(null);
    setMentionSuggestedUsers([]);
    setAbleMention(true);
    setMessageInputEvent(null);
    setShowVoiceMessageInput(false);
  }, [channel === null || channel === void 0 ? void 0 : channel.url]);
  React.useEffect(function () {
    if ((mentionedUsers === null || mentionedUsers === void 0 ? void 0 : mentionedUsers.length) >= maxUserMentionCount) {
      setAbleMention(false);
    } else {
      setAbleMention(true);
    }
  }, [mentionedUsers]);
  React.useEffect(function () {
    setMentionedUsers(mentionedUsers.filter(function (_a) {
      var userId = _a.userId;
      var 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".concat(showVoiceMessageInput ? '--voice-message' : '')
  }, displaySuggestedMentionList && /*#__PURE__*/React__default["default"].createElement(Channel_components_SuggestedMentionList, {
    targetNickname: mentionNickname,
    inputEvent: messageInputEvent,
    renderUserMentionItem: renderUserMentionItem,
    onUserItemClick: function (user) {
      if (user) {
        setMentionedUsers(tslib_es6.__spreadArray(tslib_es6.__spreadArray([], mentionedUsers, true), [user], false));
      }

      setMentionNickname('');
      setSelectedUser(user);
      setMessageInputEvent(null);
    },
    onFocusItemChange: function () {
      setMessageInputEvent(null);
    },
    onFetchUsers: function (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: function () {
      return setQuoteMessage(null);
    }
  })), showVoiceMessageInput ? /*#__PURE__*/React__default["default"].createElement(VoiceMessageInputWrapper.VoiceMessageInputWrapper, {
    channel: channel,
    onSubmitClick: function (recordedFile, duration) {
      sendVoiceMessage(recordedFile, duration, quoteMessage);
      setQuoteMessage(null);
      setShowVoiceMessageInput(false);
    },
    onCancelClick: function () {
      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: function () {
      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: function () {
      channel === null || channel === void 0 ? void 0 : channel.startTyping();
    },
    onSendMessage: function (_a) {
      var _b;

      var message = _a.message,
          mentionTemplate = _a.mentionTemplate;
      sendMessage({
        message: message,
        quoteMessage: quoteMessage,
        mentionedUsers: mentionedUsers,
        mentionTemplate: mentionTemplate
      });
      setMentionNickname('');
      setMentionedUsers([]);
      setQuoteMessage(null);
      (_b = channel === null || channel === void 0 ? void 0 : channel.endTyping) === null || _b === void 0 ? void 0 : _b.call(channel);
    },
    onFileUpload: function (file) {
      sendFileMessage(file, quoteMessage);
      setQuoteMessage(null);
    },
    onUserMentioned: function (user) {
      if ((selectedUser === null || selectedUser === void 0 ? void 0 : selectedUser.userId) === (user === null || user === void 0 ? void 0 : user.userId)) {
        setSelectedUser(null);
        setMentionNickname('');
      }
    },
    onMentionStringChange: function (mentionText) {
      setMentionNickname(mentionText);
    },
    onMentionedUserIdsUpdated: function (userIds) {
      setMentionedUserIds(userIds);
    },
    onKeyDown: function (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
