import React__default, { useMemo } from 'react';
import ThreadListItem from './ThreadListItem.js';
import { u as useThreadContext, c as compareMessagesForGrouping } from '../../ThreadProvider-b6a76698.js';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import { isSameDay } from 'date-fns';
import '../../tslib.es6-ea477498.js';
import '../../index-a8115de7.js';
import '../../index-14786153.js';
import '../../LocalizationContext-299b1d45.js';
import '../../stringSet-8039f73a.js';
import '../../ui/DateSeparator.js';
import '../../color-a6ad5b2f.js';
import '../../index-256d00f1.js';
import 'prop-types';
import '../../RemoveMessageModal-2f571cc3.js';
import '../../ui/Modal.js';
import 'react-dom';
import '../../index-f0cb5427.js';
import '../../ui/Button.js';
import '../../ui/Icon.js';
import '../../ui/IconButton.js';
import '../../MediaQueryContext-84b087e0.js';
import '../../ui/FileViewer.js';
import '../../index-b16ab63e.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../ui/Avatar.js';
import '../../ui/ImageRenderer.js';
import '../../uuid-2d756af4.js';
import '../../Channel/components/SuggestedMentionList.js';
import '../../ChannelProvider-e50a1789.js';
import '../../UserProfileContext-53d89b5e.js';
import '../../topics-948f8346.js';
import '../../_rollupPluginBabelHelpers-d746227b.js';
import '../../compareIds-4e3d3f6c.js';
import '../../const-40f65aaf.js';
import '@sendbird/chat/groupChannel';
import '@sendbird/chat/message';
import '../../ui/ContextMenu.js';
import '../../ui/SortByRow.js';
import '../../ui/ReactionButton.js';
import '../../const-c982283f.js';
import '../../withSendbird.js';
import '../context/types.js';
import '@sendbird/chat';
import '../../ui/MessageInput.js';
import 'react-dom/server';
import '../../ui/MentionUserLabel.js';
import '../../ui/UserProfile.js';
import '../../sendbirdSelectors.js';
import '../../utils-437f34f2.js';
import '../../ui/MessageItemMenu.js';
import '../../ui/MessageItemReactionMenu.js';
import '../../index-098867a9.js';
import '../../ui/Loader.js';
import '../../index-856dc0bd.js';
import '../../index-58485e08.js';
import '../../ui/EmojiReactions.js';
import '../../ui/Tooltip.js';
import '../../ui/TooltipWrapper.js';
import '../../ui/ReactionBadge.js';
import '../../ui/TextMessageItemBody.js';
import '../../ui/Word.js';
import '../../ui/LinkLabel.js';
import '../../ui/MentionLabel.js';
import '../../ui/OGMessageItemBody.js';
import '../../ui/FileMessageItemBody.js';
import '../../ui/TextButton.js';
import '../../ui/ThumbnailMessageItemBody.js';
import '../../ui/UnknownMessageItemBody.js';

function ThreadList(_a) {
  var className = _a.className,
      allThreadMessages = _a.allThreadMessages,
      renderMessage = _a.renderMessage,
      renderCustomSeparator = _a.renderCustomSeparator,
      scrollRef = _a.scrollRef,
      scrollBottom = _a.scrollBottom;
  var config = useSendbirdStateContext().config;
  var replyType = config.replyType;
  var currentChannel = useThreadContext().currentChannel;
  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 /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-thread-list ".concat(className)
  }, allThreadMessages.map(function (message, idx) {
    var prevMessage = allThreadMessages[idx - 1];
    var nextMessage = allThreadMessages[idx + 1];

    var _a = compareMessagesForGrouping(prevMessage, message, nextMessage, currentChannel, replyType) ,
        chainTop = _a[0],
        chainBottom = _a[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 MemorizedMessage({
      message: message,
      chainTop: chainTop,
      chainBottom: chainBottom,
      hasSeparator: hasSeparator
    }) || /*#__PURE__*/React__default.createElement(ThreadListItem, {
      message: message,
      chainTop: chainTop,
      chainBottom: chainBottom,
      hasSeparator: hasSeparator,
      renderCustomSeparator: renderCustomSeparator,
      handleScroll: handleScroll
    });
  }));
}

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