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-0vtSUcHY.js';
import '../../chunks/bundle-IKRolu9P.js';
import '../../chunks/bundle-YOec1wfw.js';
import '../../chunks/bundle-zOkaSZl3.js';
import '../../chunks/bundle-_NUeDv_m.js';
import '../../ui/DateSeparator.js';
import '../../chunks/bundle-3hGhEhNx.js';
import '../../chunks/bundle-RzWTDZDo.js';
import '../../chunks/bundle-WSNk7jGd.js';
import '../../chunks/bundle-92kzAxsw.js';
import 'react-dom';
import '../../chunks/bundle-4ab6nIKe.js';
import '../../chunks/bundle-1XQGq5tE.js';
import '../../ui/IconButton.js';
import '../../ui/Button.js';
import '../../ui/Icon.js';
import '../../chunks/bundle-ncF5Vx6b.js';
import '../../chunks/bundle-hrqkDKRD.js';
import '@sendbird/chat/groupChannel';
import '../../utils/message/getOutgoingMessageState.js';
import '../../chunks/bundle-9wFvMqxF.js';
import '../../chunks/bundle-7gnDs7-N.js';
import '../../hooks/useModal.js';
import '../../chunks/bundle-7RyEqo9-.js';
import '../../ui/ImageRenderer.js';
import '../../chunks/bundle-TEGbbRiE.js';
import '../../chunks/bundle-e5lT4U6_.js';
import '../../chunks/bundle-Npg4qQQD.js';
import '@sendbird/uikit-tools';
import '../../chunks/bundle-laq4Cm2M.js';
import '../../chunks/bundle-Xy9OskjJ.js';
import '../../chunks/bundle-brky8TnE.js';
import '../../chunks/bundle-K3gy6I7U.js';
import '../../ui/MessageInput.js';
import '../../chunks/bundle-QUJKjMCy.js';
import '../../chunks/bundle-fNYexgBr.js';
import 'dompurify';
import '../../chunks/bundle-72sPkBu4.js';
import '../../chunks/bundle-5x-SH0ln.js';
import '../../chunks/bundle--M9av9I5.js';
import '../../chunks/bundle-2xEG_fol.js';
import '../context/types.js';
import '../../ui/ContextMenu.js';
import '../../ui/SortByRow.js';
import '../../chunks/bundle-U_0F9z8c.js';
import '../../ui/UserProfile.js';
import '../../sendbirdSelectors.js';
import '../../chunks/bundle-KwnXXB-s.js';
import '../../withSendbird.js';
import '../../ui/MessageItemMenu.js';
import '../../chunks/bundle--4gDKFyU.js';
import '../../ui/MessageItemReactionMenu.js';
import '../../ui/ReactionButton.js';
import '../../chunks/bundle-AAE0evYg.js';
import '../../chunks/bundle-9M3o3hPA.js';
import '../../ui/Loader.js';
import '../../chunks/bundle-2lbcW98e.js';
import '../../chunks/bundle-BnMLjoeD.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 '../../ui/TextMessageItemBody.js';
import '../../chunks/bundle-zqJVlTpN.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-bAvA5Udb.js';
import '../../ui/UnknownMessageItemBody.js';
import '../../ui/VoiceMessageItemBody.js';
import '../../ui/ProgressBar.js';
import '../../VoicePlayer/useVoicePlayer.js';
import '../../chunks/bundle-KNG-9gh9.js';
import '../../VoiceRecorder/context.js';
import '../../ui/PlaybackTime.js';
import '../../chunks/bundle-kNzGDcl1.js';
import '../../chunks/bundle-FqjGVIIM.js';
import '@sendbird/chat/message';
import '../../Message/hooks/useDirtyGetMentions.js';
import '../../chunks/bundle-5cvB_UbP.js';
import '../../chunks/bundle-X1ee5Xag.js';
import '../../chunks/bundle-QWYghVSN.js';
import '@sendbird/chat';
import '../../chunks/bundle-SI8TVXcF.js';
import '../../chunks/bundle-TlXrstq8.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
