import { a as __spreadArray } from '../../chunks/bundle-B8Grfnrx.js';
import React__default, { useRef, useState, useEffect } from 'react';
import { MutedState } from '@sendbird/chat/groupChannel';
import { u as useMediaQueryContext } from '../../chunks/bundle-yKqHFFfz.js';
import { u as useLocalization } from '../../chunks/bundle-l_QLKq1Y.js';
import MessageInput from '../../ui/MessageInput.js';
import { M as MessageInputKeys } from '../../chunks/bundle-DXneAdAp.js';
import { S as SuggestedMentionList } from '../../chunks/bundle-WJtiC3BL.js';
import { V as VoiceMessageInputWrapper } from '../../chunks/bundle-uthnB1Zu.js';
import '../../chunks/bundle-C4OmfIXk.js';
import '../../chunks/bundle-BciLDE6M.js';
import { u as useSendbird } from '../../chunks/bundle-I25ah_Bv.js';
import '../../chunks/bundle-D4BlmM5H.js';
import '../../chunks/bundle-De-3zaYV.js';
import '@sendbird/chat/message';
import '../../chunks/bundle--1cImOGd.js';
import { c as classnames } from '../../chunks/bundle-CDcu1I4f.js';
import { R as Role } from '../../chunks/bundle-BfEYq-V8.js';
import { useDirtyGetMentions } from '../../Message/hooks/useDirtyGetMentions.js';
import { useHandleUploadFiles } from '../../Channel/hooks/useHandleUploadFiles.js';
import { b as isDisabledBecauseFrozen, c as isDisabledBecauseMuted } from '../../chunks/bundle-D_tSqvX6.js';
import { u as useThread } from '../../chunks/bundle-CVS5Kfg_.js';
import '../../chunks/bundle-Y2KBbxRr.js';
import '../../chunks/bundle-BYOddwmS.js';
import '../../ui/IconButton.js';
import '../../ui/Button.js';
import '../../chunks/bundle-HspRRkmy.js';
import '../../chunks/bundle-DwLTEVMK.js';
import 'dompurify';
import '../../chunks/bundle-Dp_oW1ks.js';
import '../../chunks/bundle-Cn2czFA-.js';
import '../../chunks/bundle-4w9Y7uxA.js';
import '../../ui/Icon.js';
import '../../chunks/bundle-CgLhYjm5.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../chunks/bundle-BTxw_tIt.js';
import '../../chunks/bundle-DYBTopZK.js';
import '../../chunks/bundle-Ob7Cw3V-.js';
import '../../chunks/bundle-DT1VcdMs.js';
import '../../chunks/bundle-CF8V-ET_.js';
import '../../chunks/bundle-DgFzcADJ.js';
import '../../ui/ImageRenderer.js';
import '../../chunks/bundle-C_ngoQD_.js';
import '../../chunks/bundle-CTkGIdt8.js';
import '../../chunks/bundle-D8xAej93.js';
import '../../GroupChannel/components/SuggestedMentionList.js';
import '@sendbird/chat';
import '@sendbird/chat/openChannel';
import '../../ui/QuoteMessageInput.js';
import '../../chunks/bundle-tFnb24Id.js';
import '../../VoicePlayer/useVoicePlayer.js';
import '../../chunks/bundle-zOZq2Vpt.js';
import '../../VoiceRecorder/context.js';
import '../../chunks/bundle-LnGQMQVt.js';
import 'react-dom';
import '../../VoiceRecorder/useVoiceRecorder.js';
import '../../chunks/bundle-DhzrKzt2.js';
import '../../ui/PlaybackTime.js';
import '../../ui/ProgressBar.js';
import '../../ui/TextButton.js';
import '../../chunks/bundle-DED6q4Ti.js';
import '../../hooks/useModal.js';
import '../../chunks/bundle-CSDZhWAY.js';
import '@sendbird/uikit-tools';
import '../../chunks/bundle-BYqblmEM.js';
import '../../chunks/bundle-Dx4-1s0A.js';
import '../../chunks/bundle-C6Hk5xTT.js';
import '../../chunks/bundle-B5_aP8LE.js';
import '../../chunks/bundle-C5Pz6wMm.js';
import '../../chunks/bundle-BCGcsRu2.js';
import '../../chunks/bundle-Djq12iSf.js';
import '../context/types.js';

var ThreadMessageInput = function (props, ref) {
    var _a;
    var className = props.className, renderFileUploadIcon = props.renderFileUploadIcon, renderVoiceMessageIcon = props.renderVoiceMessageIcon, renderSendMessageIcon = props.renderSendMessageIcon, acceptableMimeTypes = props.acceptableMimeTypes;
    var config = useSendbird().state.config;
    var isMobile = useMediaQueryContext().isMobile;
    var stringSet = useLocalization().stringSet;
    var isOnline = config.isOnline, userMention = config.userMention, logger = config.logger, groupChannel = config.groupChannel;
    var threadContext = useThread();
    var _b = threadContext.state, currentChannel = _b.currentChannel, parentMessage = _b.parentMessage, isMuted = _b.isMuted, isChannelFrozen = _b.isChannelFrozen, allThreadMessages = _b.allThreadMessages, _c = threadContext.actions, sendMessage = _c.sendMessage, sendFileMessage = _c.sendFileMessage, sendVoiceMessage = _c.sendVoiceMessage, sendMultipleFilesMessage = _c.sendMultipleFilesMessage;
    var messageInputRef = useRef();
    var isMentionEnabled = groupChannel.enableMention;
    var isVoiceMessageEnabled = groupChannel.enableVoiceMessage;
    var isMultipleFilesMessageEnabled = (_a = threadContext.state.isMultipleFilesMessageEnabled) !== null && _a !== void 0 ? _a : config.isMultipleFilesMessageEnabled;
    var threadInputDisabled = props.disabled
        || !isOnline
        || isMuted
        || (!((currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.myRole) === Role.OPERATOR) && isChannelFrozen) || parentMessage === null;
    // MFM
    var handleUploadFiles = useHandleUploadFiles({
        sendFileMessage: sendFileMessage,
        sendMultipleFilesMessage: sendMultipleFilesMessage,
        quoteMessage: parentMessage,
    }, {
        logger: logger,
    });
    // mention
    var _d = useState(''), mentionNickname = _d[0], setMentionNickname = _d[1];
    var _e = useState([]), mentionedUsers = _e[0], setMentionedUsers = _e[1];
    var _f = useState([]), mentionedUserIds = _f[0], setMentionedUserIds = _f[1];
    var _g = useState(null), selectedUser = _g[0], setSelectedUser = _g[1];
    var _h = useState([]), mentionSuggestedUsers = _h[0], setMentionSuggestedUsers = _h[1];
    var _j = useState(null), messageInputEvent = _j[0], setMessageInputEvent = _j[1];
    var _k = useState(false), showVoiceMessageInput = _k[0], setShowVoiceMessageInput = _k[1];
    var displaySuggestedMentionList = isOnline
        && isMentionEnabled
        && mentionNickname.length > 0
        && !isDisabledBecauseFrozen(currentChannel)
        && !isDisabledBecauseMuted(currentChannel)
        && !(currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.isBroadcast);
    // Reset when changing channel
    useEffect(function () {
        setShowVoiceMessageInput(false);
    }, [currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.url]);
    var mentionNodes = useDirtyGetMentions({ ref: ref || messageInputRef }, { logger: logger });
    var ableMention = (mentionNodes === null || mentionNodes === void 0 ? void 0 : mentionNodes.length) < (userMention === null || userMention === void 0 ? void 0 : userMention.maxMentionCount);
    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]);
    if ((currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.isBroadcast) && (currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.myRole) !== Role.OPERATOR) {
        return React__default.createElement(React__default.Fragment, null);
    }
    return (React__default.createElement("div", { className: classnames(showVoiceMessageInput ? 'sendbird-thread-message-input--voice-message' : 'sendbird-thread-message-input', className) },
        displaySuggestedMentionList && (React__default.createElement(SuggestedMentionList, { targetNickname: mentionNickname, inputEvent: messageInputEvent !== null && messageInputEvent !== void 0 ? messageInputEvent : undefined, 
            // renderUserMentionItem={renderUserMentionItem}
            onUserItemClick: function (user) {
                if (user) {
                    setMentionedUsers(__spreadArray(__spreadArray([], mentionedUsers, true), [user], false));
                }
                setMentionNickname('');
                setSelectedUser(user);
                setMessageInputEvent(null);
            }, onFocusItemChange: function () {
                setMessageInputEvent(null);
            }, onFetchUsers: function (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
            ? (React__default.createElement(VoiceMessageInputWrapper, { channel: currentChannel, onSubmitClick: function (recordedFile, duration) {
                    sendVoiceMessage(recordedFile, duration, parentMessage);
                    setShowVoiceMessageInput(false);
                }, onCancelClick: function () {
                    setShowVoiceMessageInput(false);
                } }))
            : (React__default.createElement(MessageInput, { className: "sendbird-thread-message-input__message-input", messageFieldId: "sendbird-message-input-text-field--thread", channel: currentChannel, channelUrl: currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.url, isMobile: isMobile, disabled: threadInputDisabled, acceptableMimeTypes: acceptableMimeTypes, setMentionedUsers: setMentionedUsers, mentionSelectedUser: selectedUser, isMentionEnabled: isMentionEnabled, isVoiceMessageEnabled: isVoiceMessageEnabled, isSelectingMultipleFilesEnabled: isMultipleFilesMessageEnabled, onVoiceMessageIconClick: function () {
                    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) === Role.OPERATOR) && stringSet.MESSAGE_INPUT__PLACE_HOLDER__DISABLED)
                    || ((currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.myMutedState) === 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: function () {
                    var _a;
                    (_a = currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.startTyping) === null || _a === void 0 ? void 0 : _a.call(currentChannel);
                }, onSendMessage: function (_a) {
                    var _b;
                    var message = _a.message, mentionTemplate = _a.mentionTemplate;
                    sendMessage({
                        message: message,
                        mentionedUsers: mentionedUsers,
                        mentionTemplate: mentionTemplate,
                        quoteMessage: parentMessage,
                    });
                    setMentionNickname('');
                    setMentionedUsers([]);
                    (_b = currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.endTyping) === null || _b === void 0 ? void 0 : _b.call(currentChannel);
                }, onFileUpload: handleUploadFiles, 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 === MessageInputKeys.Enter && ableMention) || e.key === MessageInputKeys.ArrowUp || e.key === MessageInputKeys.ArrowDown)) {
                        setMessageInputEvent(e);
                        return true;
                    }
                    return false;
                } }))));
};
var ThreadMessageInput$1 = React__default.forwardRef(ThreadMessageInput);

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