'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _tslib = require('../../chunks/bundle-CxoFSSXv.js');
var React = require('react');
var uikitTools = require('@sendbird/uikit-tools');
var index = require('../../chunks/bundle-D7YiKR6i.js');
var types = require('../../chunks/bundle-B-Smxy4L.js');
var ui_PlaceHolder = require('../../ui/PlaceHolder.js');
var ui_Icon = require('../../ui/Icon.js');
var GroupChannel_components_Message = require('./Message.js');
var GroupChannel_components_UnreadCount = require('./UnreadCount.js');
var GroupChannel_components_FrozenNotification = require('./FrozenNotification.js');
var consts = require('../../chunks/bundle-yU9Mr2pS.js');
var ui_TypingIndicatorBubble = require('../../ui/TypingIndicatorBubble.js');
var utils$1 = require('../../chunks/bundle-DQNu-S6D.js');
var Channel_utils_getMessagePartsInfo = require('../../Channel/utils/getMessagePartsInfo.js');
var Message_context = require('../../Message/context.js');
var utils$2 = require('../../chunks/bundle-CzJX13Yl.js');
var utils = require('../../chunks/bundle-CqFqMb6S.js');
var GroupChannelProvider = require('../../chunks/bundle-DJOukqkN.js');
var useSendbird = require('../../chunks/bundle-LI9JjqG2.js');
var LocalizationContext = require('../../chunks/bundle-DDNdw3A9.js');
require('@sendbird/chat/groupChannel');
require('../../utils/message/getOutgoingMessageState.js');
require('../../chunks/bundle-dfqZUdij.js');
require('../../chunks/bundle-Dn0hi6OJ.js');
require('../../chunks/bundle-C1G0SKDq.js');
require('../../chunks/bundle-B0UICprZ.js');
require('../../chunks/bundle-EhtiN7UV.js');
require('../../ui/Loader.js');
require('../../chunks/bundle-BV7y6VDn.js');
require('../../chunks/bundle-BX4VESNC.js');
require('../../chunks/bundle-FJtjwZwh.js');
require('../../chunks/bundle-kPv1Olqr.js');
require('../../Message/hooks/useDirtyGetMentions.js');
require('../../ui/DateSeparator.js');
require('../../chunks/bundle-BIrmQghI.js');
require('../../ui/MessageInput.js');
require('../../chunks/bundle-Cwboa2Wr.js');
require('../../ui/IconButton.js');
require('../../ui/Button.js');
require('../../chunks/bundle-Bx1_cAVo.js');
require('dompurify');
require('../../chunks/bundle-za5uPkdP.js');
require('../../chunks/bundle-CgCnWqE5.js');
require('../../chunks/bundle-BXvzT2m9.js');
require('../../chunks/bundle-CXHWkhsI.js');
require('../../chunks/bundle-AAO7jUU2.js');
require('@sendbird/chat');
require('@sendbird/chat/openChannel');
require('../../ui/MessageContent.js');
require('../../chunks/bundle-BFKxvlzn.js');
require('../../chunks/bundle-CBLzrxjV.js');
require('../../chunks/bundle-B9BY1g5d.js');
require('../../chunks/bundle-D2TXT2xX.js');
require('../../chunks/bundle-Bqahh-7Q.js');
require('react-dom');
require('../../ui/SortByRow.js');
require('../../chunks/bundle-BUGwK1WK.js');
require('../../ui/MessageItemReactionMenu.js');
require('../../ui/ImageRenderer.js');
require('../../ui/ReactionButton.js');
require('../../chunks/bundle-Bd-cNaJc.js');
require('../../chunks/bundle-CyQD5TkW.js');
require('../../ui/EmojiReactions.js');
require('../../ui/ReactionBadge.js');
require('../../ui/BottomSheet.js');
require('../../hooks/useModal.js');
require('../../chunks/bundle-YeW89zsR.js');
require('../../ui/UserListItem.js');
require('../../chunks/bundle-DeyX1dvY.js');
require('../../chunks/bundle-CLL-nCFU.js');
require('../../chunks/bundle-C8MACRPO.js');
require('../../ui/MutedAvatarOverlay.js');
require('../../ui/Checkbox.js');
require('../../ui/UserProfile.js');
require('../../sendbirdSelectors.js');
require('../../chunks/bundle-BPWJgESF.js');
require('../../ui/Tooltip.js');
require('../../ui/TooltipWrapper.js');
require('../../chunks/bundle-DsZL8mH-.js');
require('../../ui/AdminMessage.js');
require('../../ui/QuoteMessage.js');
require('../../chunks/bundle-BPGU6BH2.js');
require('@sendbird/chat/message');
require('../../ui/MobileMenu.js');
require('../../ui/ThreadReplies.js');
require('../../chunks/bundle-G4J-OWth.js');
require('../../ui/OGMessageItemBody.js');
require('../../chunks/bundle-vEi_Cfco.js');
require('../../ui/MentionLabel.js');
require('../../ui/LinkLabel.js');
require('../../ui/TextMessageItemBody.js');
require('../../ui/FileMessageItemBody.js');
require('../../ui/TextButton.js');
require('../../chunks/bundle-Cq2Y8hPN.js');
require('../../ui/FileViewer.js');
require('../../chunks/bundle-DNjcGEfJ.js');
require('../../ui/VoiceMessageItemBody.js');
require('../../ui/ProgressBar.js');
require('../../VoicePlayer/useVoicePlayer.js');
require('../../chunks/bundle-DK39NcVd.js');
require('../../VoiceRecorder/context.js');
require('../../ui/PlaybackTime.js');
require('../../ui/ThumbnailMessageItemBody.js');
require('../../ui/UnknownMessageItemBody.js');
require('../../ui/TemplateMessageItemBody.js');
require('../../chunks/bundle-aMt_60_o.js');
require('../../ui/FallbackTemplateMessageItemBody.tsx.js');
require('../../ui/LoadingTemplateMessageItemBody.tsx.js');
require('../../ui/MessageFeedbackFailedModal.js');
require('../../ui/FeedbackIconButton.js');
require('../../ui/MobileFeedbackMenu.js');
require('../../ui/MessageFeedbackModal.js');
require('../../ui/Input.js');
require('../../chunks/bundle-CPeZ4wPf.js');
require('./SuggestedReplies.js');
require('../../chunks/bundle-DptJHWLW.js');
require('./FileViewer.js');
require('../../chunks/bundle-CaNXlSAv.js');
require('../../chunks/bundle-B2KoWBOt.js');
require('../../chunks/bundle-CG2rv5Ij.js');
require('../../chunks/bundle-ChpcAmBg.js');
require('./RemoveMessageModal.js');
require('../../chunks/bundle-L8U6SolV.js');
require('../../chunks/bundle-C4t_1F0C.js');
require('../../Channel/utils/compareMessagesForGrouping.js');

function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefaultCompat(React);

var InfiniteList = React.forwardRef(function (props, listRef) {
    var messages = props.messages, renderMessage = props.renderMessage, scrollPositionRef = props.scrollPositionRef, scrollDistanceFromBottomRef = props.scrollDistanceFromBottomRef, onLoadPrevious = props.onLoadPrevious, onLoadNext = props.onLoadNext, _a = props.loadThreshold, loadThreshold = _a === void 0 ? 0.05 : _a, typingIndicator = props.typingIndicator, _b = props.onScrollPosition, onScrollPosition = _b === void 0 ? utils$1.noop : _b, initDeps = props.initDeps;
    var isFetching = React__default.default.useRef(false);
    var direction = React__default.default.useRef();
    var oldScrollTop = React.useRef(0);
    // SideEffect: scroll to bottom on initialized
    React.useLayoutEffect(function () {
        if (listRef.current) {
            listRef.current.scrollTop = listRef.current.scrollHeight;
        }
    }, initDeps);
    // SideEffect: keep scroll position
    React.useLayoutEffect(function () {
        if (listRef.current) {
            if (direction.current === 'top') {
                listRef.current.scrollTop = listRef.current.scrollHeight - scrollPositionRef.current;
            }
            if (direction.current === 'bottom') {
                listRef.current.scrollTop = oldScrollTop.current;
            }
            direction.current = undefined;
        }
    }, [listRef.current, messages.length]);
    var handleScroll = React.useCallback(function () { return _tslib.__awaiter(void 0, void 0, void 0, function () {
        var list, threshold;
        return _tslib.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    if (!listRef.current)
                        return [2 /*return*/];
                    list = listRef.current;
                    onScrollPosition(getReachedStatus(list));
                    scrollPositionRef.current = list.scrollHeight - list.scrollTop;
                    scrollDistanceFromBottomRef.current = scrollPositionRef.current - list.clientHeight;
                    oldScrollTop.current = list.scrollTop;
                    if (isFetching.current)
                        return [2 /*return*/];
                    threshold = list.clientHeight * Math.min(Math.max(0, loadThreshold), 1);
                    if (!(list.scrollTop <= threshold)) return [3 /*break*/, 2];
                    isFetching.current = true;
                    direction.current = 'top';
                    return [4 /*yield*/, onLoadPrevious()];
                case 1:
                    _a.sent();
                    isFetching.current = false;
                    return [3 /*break*/, 5];
                case 2:
                    if (!(list.scrollHeight - list.scrollTop - list.clientHeight <= threshold)) return [3 /*break*/, 4];
                    isFetching.current = true;
                    direction.current = 'bottom';
                    return [4 /*yield*/, onLoadNext()];
                case 3:
                    _a.sent();
                    isFetching.current = false;
                    return [3 /*break*/, 5];
                case 4:
                    direction.current = undefined;
                    _a.label = 5;
                case 5: return [2 /*return*/];
            }
        });
    }); }, [messages.length]);
    return (React__default.default.createElement("div", { className: "sendbird-conversation__scroll-container" },
        React__default.default.createElement("div", { className: "sendbird-conversation__padding" }),
        React__default.default.createElement("div", { ref: listRef, className: "sendbird-conversation__messages-padding", "data-testid": "sendbird-message-list-container", onScroll: handleScroll },
            messages.map(function (message, index) { return renderMessage({ message: message, index: index }); }),
            typingIndicator)));
});
function getReachedStatus(element) {
    if (utils.isAboutSame(element.scrollTop, 0, consts.SCROLL_BUFFER)) {
        return 'top';
    }
    if (utils.isAboutSame(element.scrollHeight, element.clientHeight + element.scrollTop, consts.SCROLL_BUFFER)) {
        return 'bottom';
    }
    return 'middle';
}

var MessageList = function (props) {
    var _a, _b, _c, _d, _e;
    var _f = props.className, className = _f === void 0 ? '' : _f;
    var _g = utils$1.deleteNullish(props), _h = _g.renderMessage, renderMessage = _h === void 0 ? function (props) { return React__default.default.createElement(GroupChannel_components_Message.Message, _tslib.__assign({}, props)); } : _h, renderMessageContent = _g.renderMessageContent, renderSuggestedReplies = _g.renderSuggestedReplies, renderCustomSeparator = _g.renderCustomSeparator, _j = _g.renderPlaceholderLoader, renderPlaceholderLoader = _j === void 0 ? function () { return React__default.default.createElement(ui_PlaceHolder.default, { type: ui_PlaceHolder.PlaceHolderTypes.LOADING }); } : _j, _k = _g.renderPlaceholderEmpty, renderPlaceholderEmpty = _k === void 0 ? function () { return React__default.default.createElement(ui_PlaceHolder.default, { className: "sendbird-conversation__no-messages", type: ui_PlaceHolder.PlaceHolderTypes.NO_MESSAGES }); } : _k, _l = _g.renderFrozenNotification, renderFrozenNotification = _l === void 0 ? function () { return React__default.default.createElement(GroupChannel_components_FrozenNotification.FrozenNotification, { className: "sendbird-conversation__messages__notification" }); } : _l;
    var _m = GroupChannelProvider.useGroupChannel(), _o = _m.state, channelUrl = _o.channelUrl, hasNext = _o.hasNext, loading = _o.loading, messages = _o.messages, newMessages = _o.newMessages, isScrollBottomReached = _o.isScrollBottomReached, isMessageGroupingEnabled = _o.isMessageGroupingEnabled, currentChannel = _o.currentChannel, replyType = _o.replyType, scrollPubSub = _o.scrollPubSub, loadNext = _o.loadNext, loadPrevious = _o.loadPrevious, resetNewMessages = _o.resetNewMessages, scrollRef = _o.scrollRef, scrollPositionRef = _o.scrollPositionRef, scrollDistanceFromBottomRef = _o.scrollDistanceFromBottomRef, _p = _m.actions, scrollToBottom = _p.scrollToBottom, setIsScrollBottomReached = _p.setIsScrollBottomReached;
    var state = useSendbird.useSendbird().state;
    var stringSet = LocalizationContext.useLocalization().stringSet;
    var _q = React.useState(), unreadSinceDate = _q[0], setUnreadSinceDate = _q[1];
    React.useEffect(function () {
        if (isScrollBottomReached) {
            setUnreadSinceDate(undefined);
        }
        else {
            setUnreadSinceDate(new Date());
        }
    }, [isScrollBottomReached]);
    /**
     * 1. Move the message list scroll
     *    when each message's height is changed by `reactions` OR `showEdit`
     * 2. Keep the scrollBottom value after fetching new message list
     */
    var onMessageContentSizeChanged = function (isBottomMessageAffected) {
        if (isBottomMessageAffected === void 0) { isBottomMessageAffected = false; }
        var elem = scrollRef.current;
        if (elem) {
            var latestDistance = scrollDistanceFromBottomRef.current;
            var currentDistance = elem.scrollHeight - elem.scrollTop - elem.offsetHeight;
            if (latestDistance < currentDistance && (!isBottomMessageAffected || latestDistance < consts.SCROLL_BUFFER)) {
                var diff = currentDistance - latestDistance;
                // Move the scroll as much as the height of the message has changed
                scrollPubSub.publish('scroll', { top: elem.scrollTop + diff, lazy: false, animated: false });
            }
        }
    };
    var renderer = {
        frozenNotification: function () {
            if (!currentChannel || !currentChannel.isFrozen)
                return null;
            return renderFrozenNotification();
        },
        unreadMessagesNotification: function () {
            if (isScrollBottomReached || !unreadSinceDate)
                return null;
            return (React__default.default.createElement(GroupChannel_components_UnreadCount.UnreadCount, { className: "sendbird-conversation__messages__notification", count: newMessages.length, lastReadAt: unreadSinceDate, onClick: function () { return scrollToBottom(); } }));
        },
        scrollToBottomButton: function () {
            if (!hasNext() && isScrollBottomReached)
                return null;
            return (React__default.default.createElement("div", { className: "sendbird-conversation__scroll-bottom-button", onClick: function () { return scrollToBottom(); }, onKeyDown: function () { return scrollToBottom(); }, tabIndex: 0, role: "button" },
                React__default.default.createElement(ui_Icon.default, { width: "24px", height: "24px", type: ui_Icon.IconTypes.CHEVRON_DOWN, fillColor: ui_Icon.IconColors.PRIMARY })));
        },
    };
    if (loading) {
        return renderPlaceholderLoader();
    }
    if (messages.length === 0) {
        return renderPlaceholderEmpty();
    }
    return (React__default.default.createElement(React__default.default.Fragment, null,
        React__default.default.createElement("div", { className: "sendbird-conversation__messages ".concat(className), dir: index.getHTMLTextDirection(state.config.htmlTextDirection, state.config.forceLeftToRightMessageLayout) },
            React__default.default.createElement(InfiniteList, { ref: scrollRef, initDeps: [channelUrl], scrollPositionRef: scrollPositionRef, scrollDistanceFromBottomRef: scrollDistanceFromBottomRef, onLoadNext: loadNext, onLoadPrevious: loadPrevious, onScrollPosition: function (it) {
                    var isScrollBottomReached = it === 'bottom';
                    if (newMessages.length > 0 && isScrollBottomReached) {
                        resetNewMessages();
                    }
                    setIsScrollBottomReached(isScrollBottomReached);
                }, messages: messages, renderMessage: function (_a) {
                    var message = _a.message, index$1 = _a.index;
                    var _b = Channel_utils_getMessagePartsInfo.getMessagePartsInfo({
                        allMessages: messages,
                        stringSet: stringSet,
                        replyType: replyType !== null && replyType !== void 0 ? replyType : 'NONE',
                        isMessageGroupingEnabled: isMessageGroupingEnabled !== null && isMessageGroupingEnabled !== void 0 ? isMessageGroupingEnabled : false,
                        currentIndex: index$1,
                        currentMessage: message,
                        currentChannel: currentChannel,
                    }), chainTop = _b.chainTop, chainBottom = _b.chainBottom, hasSeparator = _b.hasSeparator;
                    var isOutgoingMessage = index.isSendableMessage(message) && message.sender.userId === state.config.userId;
                    return (React__default.default.createElement(Message_context.MessageProvider, { message: message, key: utils$2.getComponentKeyFromMessage(message), isByMe: isOutgoingMessage }, renderMessage({
                        handleScroll: onMessageContentSizeChanged,
                        message: message,
                        hasSeparator: hasSeparator,
                        chainTop: chainTop,
                        chainBottom: chainBottom,
                        renderMessageContent: renderMessageContent,
                        renderSuggestedReplies: renderSuggestedReplies,
                        renderCustomSeparator: renderCustomSeparator,
                    })));
                }, typingIndicator: !hasNext()
                    && ((_b = (_a = state === null || state === void 0 ? void 0 : state.config) === null || _a === void 0 ? void 0 : _a.groupChannel) === null || _b === void 0 ? void 0 : _b.enableTypingIndicator)
                    && ((_e = (_d = (_c = state === null || state === void 0 ? void 0 : state.config) === null || _c === void 0 ? void 0 : _c.groupChannel) === null || _d === void 0 ? void 0 : _d.typingIndicatorTypes) === null || _e === void 0 ? void 0 : _e.has(types.TypingIndicatorType.Bubble)) && (React__default.default.createElement(TypingIndicatorBubbleWrapper, { channelUrl: channelUrl, handleScroll: onMessageContentSizeChanged })) }),
            React__default.default.createElement(React__default.default.Fragment, null, renderer.frozenNotification()),
            React__default.default.createElement(React__default.default.Fragment, null, renderer.unreadMessagesNotification()),
            React__default.default.createElement(React__default.default.Fragment, null, renderer.scrollToBottomButton()))));
};
var TypingIndicatorBubbleWrapper = function (props) {
    var stores = useSendbird.useSendbird().state.stores;
    var _a = GroupChannelProvider.useGroupChannel().state, isScrollBottomReached = _a.isScrollBottomReached, scrollPubSub = _a.scrollPubSub;
    var _b = React.useState([]), typingMembers = _b[0], setTypingMembers = _b[1];
    uikitTools.useGroupChannelHandler(stores.sdkStore.sdk, {
        onTypingStatusUpdated: function (channel) {
            if (channel.url === props.channelUrl) {
                setTypingMembers(channel.getTypingUsers());
            }
            if (isScrollBottomReached && utils$2.isContextMenuClosed()) {
                setTimeout(function () {
                    scrollPubSub.publish('scrollToBottom', {});
                }, 10);
            }
        },
    });
    return React__default.default.createElement(ui_TypingIndicatorBubble, { typingMembers: typingMembers, handleScroll: props.handleScroll });
};

exports.MessageList = MessageList;
exports.default = MessageList;
//# sourceMappingURL=MessageList.js.map
