import React__default, { useMemo } from 'react';
import ThreadListItem from './ThreadListItem.js';
import { u as useThreadContext, c as compareMessagesForGrouping } from '../../ThreadProvider-e64f95cc.js';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import { isSameDay } from 'date-fns';
import '../../tslib.es6-e1c2b6be.js';
import '../../index-99933718.js';
import '../../index-c10fcd31.js';
import '../../LocalizationContext-8725e9b2.js';
import '../../stringSet-a513039b.js';
import '../../ui/DateSeparator.js';
import '../../color-f6b2bdb7.js';
import '../../index-e121c395.js';
import 'prop-types';
import '../../RemoveMessageModal-9b81dfb2.js';
import '../../ui/Modal.js';
import 'react-dom';
import '../../index-10a05022.js';
import '../../ui/Button.js';
import '../../ui/Icon.js';
import '../../ui/IconButton.js';
import '../../MediaQueryContext-ceb727c7.js';
import '../../ui/FileViewer.js';
import '../../index-43834bc0.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../ui/Avatar.js';
import '../../ui/ImageRenderer.js';
import '../../uuid-d072af26.js';
import '../../Channel/components/SuggestedMentionList.js';
import '../../ChannelProvider-c1c00018.js';
import '../../UserProfileContext-cc71f901.js';
import '../../topics-a581acbb.js';
import '../../_rollupPluginBabelHelpers-945410b2.js';
import '../../compareIds-87972c5f.js';
import '../../const-85535a89.js';
import '@sendbird/chat/groupChannel';
import '@sendbird/chat/message';
import '../../ui/ContextMenu.js';
import '../../ui/SortByRow.js';
import '../../ui/ReactionButton.js';
import '../../consts-59f719d8.js';
import '../../const-0f759572.js';
import '../../withSendbird.js';
import '../context/types.js';
import '@sendbird/chat';
import '../../ui/MessageInput.js';
import 'react-dom/server';
import '../../ui/MentionUserLabel.js';
import 'dompurify';
import '../../ui/UserProfile.js';
import '../../sendbirdSelectors.js';
import '../../utils-b439eac3.js';
import '../../ui/MessageItemMenu.js';
import '../../types-5cc94910.js';
import '../../ui/MessageItemReactionMenu.js';
import '../../index-1ac4456b.js';
import '../../ui/Loader.js';
import '../../index-a1a19702.js';
import '../../index-2988897b.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';
import '../../ui/VoiceMessageItemBody.js';
import '../../ui/ProgressBar.js';
import '../../VoicePlayer/useVoicePlayer.js';
import '../../index-ac5a0725.js';
import '../../VoiceRecorder/context.js';
import '../../lame.all.js';
import '../../ui/PlaybackTime.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
