import React__default, { useContext, useState, useEffect } from 'react';
import { u as useChannelContext, a as isDisabledBecauseFrozen, b as isDisabledBecauseMuted, d as isOperator } from '../../ChannelProvider-b41fde4c.js';
import MessageInput from '../../ui/MessageInput.js';
import QuoteMessageInput from '../../ui/QuoteMessageInput.js';
import { a as LocalizationContext } from '../../LocalizationContext-57b46f6c.js';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import SuggestedMentionList from './SuggestedMentionList.js';
import { M as MessageInputKeys } from '../../const-8daee9fc.js';
import { V as VoiceMessageInputWrapper } from '../../VoiceMessageInputWrapper-bdc53de4.js';
import { u as useDirtyGetMentions } from '../../useDirtyGetMentions-aac697f4.js';
import { u as useMediaQueryContext } from '../../MediaQueryContext-fbe7af77.js';
import '../../UserProfileContext-29074346.js';
import 'prop-types';
import '../../index-f1701c11.js';
import '../../index-865112db.js';
import '../../topics-5ff88d6f.js';
import '../../index-e2601165.js';
import '../../_rollupPluginBabelHelpers-0bc76382.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../compareIds-bc881fe7.js';
import '../../const-e51dda0c.js';
import '@sendbird/chat/groupChannel';
import '../../uuid-1f1bbd9c.js';
import '@sendbird/chat/message';
import '../../consts-3c0ff1ed.js';
import '../../ui/IconButton.js';
import '../../ui/Button.js';
import '../../index-4023d1b2.js';
import '../../stringSet-7a8ce1ce.js';
import 'dompurify';
import '../../consts-d0b365df.js';
import '../../ui/Icon.js';
import '../../consts-d0cfee49.js';
import '../../consts-7aab5aff.js';
import '../../tokenize-003bf3f7.js';
import '../../ui/ImageRenderer.js';
import '../../withSendbird.js';
import '../../ui/Avatar.js';
import '../../ThreadProvider-a3d3b241.js';
import '../../Thread/context/types.js';
import '@sendbird/chat';
import '../../VoicePlayer/useVoicePlayer.js';
import '../../index-d847a382.js';
import '../../VoiceRecorder/context.js';
import '../../VoiceRecorder/useVoiceRecorder.js';
import '../../index-1e5cf284.js';
import '../../ui/PlaybackTime.js';
import '../../ui/ProgressBar.js';
import '../../ui/TextButton.js';
import '../../color-2ed716e6.js';
import '../../ui/Modal.js';
import 'react-dom';
import '../../index-7bff4ce0.js';

const MessageInputWrapper = (props, ref) => {
  const {
    value,
    renderFileUploadIcon,
    renderVoiceMessageIcon,
    renderSendMessageIcon
  } = props;
  const propDisabled = props.disabled;
  const {
    currentGroupChannel,
    initialized,
    quoteMessage,
    sendMessage,
    sendFileMessage,
    sendVoiceMessage,
    setQuoteMessage,
    messageInputRef,
    renderUserMentionItem
  } = useChannelContext();
  const globalStore = useSendbirdStateContext();
  const {
    isMobile
  } = 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
  } = useContext(LocalizationContext);
  const [mentionNickname, setMentionNickname] = useState('');
  // todo: set type
  const [mentionedUsers, setMentionedUsers] = useState([]);
  const [mentionedUserIds, setMentionedUserIds] = useState([]);
  const [selectedUser, setSelectedUser] = useState(null);
  const [mentionSuggestedUsers, setMentionSuggestedUsers] = useState([]);
  const [messageInputEvent, setMessageInputEvent] = useState(null);
  const [showVoiceMessageInput, setShowVoiceMessageInput] = useState(false);
  const disabled = propDisabled || !initialized || isDisabledBecauseFrozen(channel) || isDisabledBecauseMuted(channel) || !isOnline;
  const isOperator$1 = isOperator(channel);
  const isBroadcast = channel === null || channel === void 0 ? void 0 : channel.isBroadcast;
  const displaySuggestedMentionList = isOnline && isMentionEnabled && mentionNickname.length > 0 && !isDisabledBecauseFrozen(channel) && !isDisabledBecauseMuted(channel) && !isBroadcast;

  // Reset when channel changes
  useEffect(() => {
    setMentionNickname('');
    setMentionedUsers([]);
    setMentionedUserIds([]);
    setSelectedUser(null);
    setMentionSuggestedUsers([]);
    setMessageInputEvent(null);
    setShowVoiceMessageInput(false);
  }, [channel === null || channel === void 0 ? void 0 : channel.url]);
  const mentionNodes = useDirtyGetMentions({
    ref: ref || messageInputRef
  }, {
    logger
  });
  const ableMention = (mentionNodes === null || mentionNodes === void 0 ? void 0 : mentionNodes.length) < maxUserMentionCount;
  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$1) {
    return null;
  }
  // other conditions
  return /*#__PURE__*/React__default.createElement("div", {
    className: `sendbird-message-input-wrapper${showVoiceMessageInput ? '--voice-message' : ''}`
  }, displaySuggestedMentionList && /*#__PURE__*/React__default.createElement(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.createElement("div", {
    className: "sendbird-message-input-wrapper__quote-message-input"
  }, /*#__PURE__*/React__default.createElement(QuoteMessageInput, {
    replyingMessage: quoteMessage,
    onClose: () => setQuoteMessage(null)
  })), showVoiceMessageInput ? /*#__PURE__*/React__default.createElement(VoiceMessageInputWrapper, {
    channel: channel,
    onSubmitClick: (recordedFile, duration) => {
      sendVoiceMessage(recordedFile, duration, quoteMessage);
      setQuoteMessage(null);
      setShowVoiceMessageInput(false);
    },
    onCancelClick: () => {
      setShowVoiceMessageInput(false);
    }
  }) : /*#__PURE__*/React__default.createElement(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 || isDisabledBecauseFrozen(channel) && stringSet.MESSAGE_INPUT__PLACE_HOLDER__DISABLED || 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 === MessageInputKeys.Enter && ableMention || e.key === MessageInputKeys.ArrowUp || e.key === MessageInputKeys.ArrowDown)) {
        setMessageInputEvent(e);
        return true;
      }
      return false;
    }
  }));
};
var MessageInputWrapper$1 = /*#__PURE__*/React__default.forwardRef(MessageInputWrapper);

export { MessageInputWrapper$1 as default };
//# sourceMappingURL=MessageInput.js.map
