'use strict';

var React = require('react');
var Channel_context = require('../../ChannelProvider-a05d259f.js');
var ui_MessageInput = require('../../ui/MessageInput.js');
var ui_QuoteMessageInput = require('../../ui/QuoteMessageInput.js');
var LocalizationContext = require('../../LocalizationContext-44d2f61b.js');
var useSendbirdStateContext = require('../../useSendbirdStateContext.js');
var Channel_components_SuggestedMentionList = require('./SuggestedMentionList.js');
var _const = require('../../const-bd02657f.js');
var VoiceMessageInputWrapper = require('../../VoiceMessageInputWrapper-6625f53a.js');
var useDirtyGetMentions = require('../../useDirtyGetMentions-1500af53.js');
var MediaQueryContext = require('../../MediaQueryContext-5d311a86.js');
require('../../UserProfileContext-9d843d68.js');
require('prop-types');
require('../../const-1ea78727.js');
require('../../index-892b4b7f.js');
require('../../index-88e8fdfa.js');
require('../../topics-373d2156.js');
require('../../index-1b3c69da.js');
require('../../_rollupPluginBabelHelpers-21ae158b.js');
require('../../utils/message/getOutgoingMessageState.js');
require('../../compareIds-a846891d.js');
require('@sendbird/chat/groupChannel');
require('../../uuid-dee74248.js');
require('@sendbird/chat/message');
require('../../consts-900e72b9.js');
require('../../resolvedReplyType-14fa0f85.js');
require('../../ui/IconButton.js');
require('../../ui/Button.js');
require('../../index-2bfd366f.js');
require('../../stringSet-7aebf034.js');
require('dompurify');
require('../../consts-867f1ae6.js');
require('../../ui/Icon.js');
require('../../consts-b93ef9ea.js');
require('../../consts-bce71b24.js');
require('../../tokenize-44139f1e.js');
require('../../index.module-5a6a79b6.js');
require('../../ui/ImageRenderer.js');
require('../../withSendbird.js');
require('../../ui/Avatar.js');
require('../../ThreadProvider-bac1bea8.js');
require('../../Thread/context/types.js');
require('@sendbird/chat');
require('../../VoicePlayer/useVoicePlayer.js');
require('../../index-50c4a83b.js');
require('../../VoiceRecorder/context.js');
require('../../ui/Modal.js');
require('react-dom');
require('../../index-0a446cca.js');
require('../../VoiceRecorder/useVoiceRecorder.js');
require('../../index-4d2879d6.js');
require('../../ui/PlaybackTime.js');
require('../../ui/ProgressBar.js');
require('../../ui/TextButton.js');
require('../../color-0c04efbd.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 {
    isMobile
  } = MediaQueryContext.useMediaQueryContext();
  const channel = currentGroupChannel;
  const {
    isOnline,
    isMentionEnabled,
    userMention,
    isVoiceMessageEnabled,
    logger
  } = 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 [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) && !isBroadcast;

  // Reset when channel changes
  React.useEffect(() => {
    setMentionNickname('');
    setMentionedUsers([]);
    setMentionedUserIds([]);
    setSelectedUser(null);
    setMentionSuggestedUsers([]);
    setMessageInputEvent(null);
    setShowVoiceMessageInput(false);
  }, [channel === null || channel === void 0 ? void 0 : channel.url]);
  const mentionNodes = useDirtyGetMentions.useDirtyGetMentions({
    ref: ref || messageInputRef
  }, {
    logger
  });
  const ableMention = (mentionNodes === null || mentionNodes === void 0 ? void 0 : mentionNodes.length) < maxUserMentionCount;
  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) && (isMobile ? stringSet.MESSAGE_INPUT__PLACE_HOLDER__MUTED_SHORT : 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
