import React__default, { useMemo } from 'react';
import ThreadListItem from './ThreadListItem.js';
import { useThreadContext } from '../context.js';
import { compareMessagesForGrouping } from '../../Channel/utils/compareMessagesForGrouping.js';
import { useSendbirdStateContext } from '../../useSendbirdStateContext.js';
import { isSameDay } from 'date-fns';
import { MessageProvider } from '../../Message/context.js';
import '../../chunks/bundle-CMZSpHeh.js';
import '../../chunks/bundle-BsoAxhbJ.js';
import '../../chunks/bundle-eddZycld.js';
import '../../chunks/bundle-Aca68v_Z.js';
import '../../chunks/bundle-XCQLW6rR.js';
import '../../ui/DateSeparator.js';
import '../../chunks/bundle-DrCrimot.js';
import '../../chunks/bundle-CIcpyYvw.js';
import '../../chunks/bundle-C6xB7o0l.js';
import '../../chunks/bundle-DYAiU0qn.js';
import 'react-dom';
import '../../chunks/bundle-DRN8Ufbc.js';
import '../../chunks/bundle-CAdHlM56.js';
import '../../ui/IconButton.js';
import '../../ui/Button.js';
import '../../ui/Icon.js';
import '../../chunks/bundle-CrN7EoKs.js';
import '../../chunks/bundle-BdOcjdmg.js';
import '@sendbird/chat/groupChannel';
import '../../utils/message/getOutgoingMessageState.js';
import '../../chunks/bundle-DI-PfZ0q.js';
import '../../chunks/bundle-Cyeay9b0.js';
import '../../hooks/useModal.js';
import '../../chunks/bundle-BlN1UcdM.js';
import '../../ui/ImageRenderer.js';
import '../../chunks/bundle-Bzds2Ucm.js';
import '../../chunks/bundle-Csv255vx.js';
import '../../chunks/bundle-BXieK_Uz.js';
import '@sendbird/uikit-tools';
import '../../chunks/bundle-CzOZ6n5M.js';
import '../../chunks/bundle-Dxzrjw7N.js';
import '../../chunks/bundle-GCGSXAv2.js';
import '../../chunks/bundle-DBqo_XXO.js';
import '../../chunks/bundle-B2uB4TXG.js';
import '../../ui/MessageInput.js';
import '../../chunks/bundle-CRA9P1Ar.js';
import 'dompurify';
import '../../chunks/bundle-BI7jBgEO.js';
import '../../chunks/bundle-C6a7C-OU.js';
import '../../chunks/bundle-Dorruixf.js';
import '../../chunks/bundle-CawmTzdt.js';
import '../context/types.js';
import '../../ui/ContextMenu.js';
import '@sendbird/chat';
import '@sendbird/chat/openChannel';
import '../../ui/SortByRow.js';
import '../../chunks/bundle-BH6pGtIL.js';
import '../../ui/UserProfile.js';
import '../../sendbirdSelectors.js';
import '../../chunks/bundle-DQ2TqmPV.js';
import '../../withSendbird.js';
import '../../ui/MessageItemMenu.js';
import '../../chunks/bundle-BcHXJbem.js';
import '../../ui/MessageItemReactionMenu.js';
import '../../ui/ReactionButton.js';
import '../../chunks/bundle-w_-mtnA_.js';
import '../../chunks/bundle-CL7uZG_N.js';
import '../../ui/Loader.js';
import '../../chunks/bundle-DggFD55q.js';
import '../../chunks/bundle-pMKSbn_-.js';
import '../../ui/EmojiReactions.js';
import '../../ui/ReactionBadge.js';
import '../../ui/BottomSheet.js';
import '../../ui/UserListItem.js';
import '../../ui/MutedAvatarOverlay.js';
import '../../ui/Checkbox.js';
import '../../ui/Tooltip.js';
import '../../ui/TooltipWrapper.js';
import '../../chunks/bundle-DWLEk8wY.js';
import '../../ui/TextMessageItemBody.js';
import '../../chunks/bundle-4qXDtSKj.js';
import '../../ui/MentionLabel.js';
import '../../ui/LinkLabel.js';
import '../../ui/OGMessageItemBody.js';
import '../../ui/FileMessageItemBody.js';
import '../../ui/TextButton.js';
import '../../ui/ThumbnailMessageItemBody.js';
import '../../chunks/bundle-DhKZXsZz.js';
import '../../ui/UnknownMessageItemBody.js';
import '../../ui/VoiceMessageItemBody.js';
import '../../ui/ProgressBar.js';
import '../../VoicePlayer/useVoicePlayer.js';
import '../../chunks/bundle-BHpijKgj.js';
import '../../VoiceRecorder/context.js';
import '../../ui/PlaybackTime.js';
import '../../chunks/bundle--4VpdkOk.js';
import '../../chunks/bundle-B5Cdyj9S.js';
import '@sendbird/chat/message';
import '../../Message/hooks/useDirtyGetMentions.js';
import '../../chunks/bundle-DOoLmQn1.js';
import '../../chunks/bundle-9-bZdwX6.js';
import '../../chunks/bundle-DsQ2We8f.js';
import '../../chunks/bundle-BF8Qdge0.js';

function ThreadList(_a) {
    var className = _a.className, renderMessage = _a.renderMessage, renderCustomSeparator = _a.renderCustomSeparator, scrollRef = _a.scrollRef, scrollBottom = _a.scrollBottom;
    var config = useSendbirdStateContext().config;
    var replyType = config.replyType, userId = config.userId;
    var _b = useThreadContext(), currentChannel = _b.currentChannel, allThreadMessages = _b.allThreadMessages, localThreadMessages = _b.localThreadMessages;
    var MemorizedMessage = useMemo(function () { return function (_a) {
        var message = _a.message, chainTop = _a.chainTop, chainBottom = _a.chainBottom, hasSeparator = _a.hasSeparator;
        if (typeof renderMessage === 'function') {
            return renderMessage({
                message: message,
                chainTop: chainTop,
                chainBottom: chainBottom,
                hasSeparator: hasSeparator,
            });
        }
        return null;
    }; }, [renderMessage]);
    return (React__default.createElement("div", { className: "sendbird-thread-list ".concat(className) },
        allThreadMessages.map(function (message, idx) {
            var _a;
            var isByMe = ((_a = message === null || message === void 0 ? void 0 : message.sender) === null || _a === void 0 ? void 0 : _a.userId) === userId;
            var prevMessage = allThreadMessages[idx - 1];
            var nextMessage = allThreadMessages[idx + 1];
            // eslint-disable-next-line no-constant-condition
            var _b = compareMessagesForGrouping(prevMessage, message, nextMessage, currentChannel, replyType)
                , chainTop = _b[0], chainBottom = _b[1];
            var hasSeparator = !((prevMessage === null || prevMessage === void 0 ? void 0 : prevMessage.createdAt) > 0 && (isSameDay(message === null || message === void 0 ? void 0 : message.createdAt, prevMessage === null || prevMessage === void 0 ? void 0 : prevMessage.createdAt)));
            var handleScroll = function () {
                var current = scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current;
                if (current) {
                    var bottom = current.scrollHeight - current.scrollTop - current.offsetHeight;
                    if (scrollBottom < bottom) {
                        current.scrollTop += bottom - scrollBottom;
                    }
                }
            };
            return (React__default.createElement(MessageProvider, { message: message, isByMe: isByMe, key: message === null || message === void 0 ? void 0 : message.messageId }, MemorizedMessage({
                message: message,
                chainTop: chainTop,
                chainBottom: chainBottom,
                hasSeparator: hasSeparator,
            }) || (React__default.createElement(ThreadListItem, { message: message, chainTop: chainTop, chainBottom: chainBottom, hasSeparator: hasSeparator, renderCustomSeparator: renderCustomSeparator, handleScroll: handleScroll }))));
        }),
        localThreadMessages.map(function (message, idx) {
            var _a;
            var isByMe = ((_a = message === null || message === void 0 ? void 0 : message.sender) === null || _a === void 0 ? void 0 : _a.userId) === userId;
            var prevMessage = localThreadMessages[idx - 1];
            var nextMessage = localThreadMessages[idx + 1];
            // eslint-disable-next-line no-constant-condition
            var _b = compareMessagesForGrouping(prevMessage, message, nextMessage, currentChannel, replyType)
                , chainTop = _b[0], chainBottom = _b[1];
            var hasSeparator = !((prevMessage === null || prevMessage === void 0 ? void 0 : prevMessage.createdAt) > 0 && (isSameDay(message === null || message === void 0 ? void 0 : message.createdAt, prevMessage === null || prevMessage === void 0 ? void 0 : prevMessage.createdAt)));
            var handleScroll = function () {
                var current = scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current;
                if (current) {
                    var bottom = current.scrollHeight - current.scrollTop - current.offsetHeight;
                    if (scrollBottom < bottom) {
                        current.scrollTop += bottom - scrollBottom;
                    }
                }
            };
            return (React__default.createElement(MessageProvider, { message: message, isByMe: isByMe, key: message === null || message === void 0 ? void 0 : message.messageId }, MemorizedMessage({
                message: message,
                chainTop: chainTop,
                chainBottom: chainBottom,
                hasSeparator: hasSeparator,
            }) || (React__default.createElement(ThreadListItem, { message: message, hasSeparator: false, renderCustomSeparator: renderCustomSeparator, handleScroll: handleScroll }))));
        })));
}

export { ThreadList as default };
//# sourceMappingURL=ThreadList.js.map
