import React__default, { useMemo } from 'react';
import ThreadListItem from './ThreadListItem.js';
import { u as useThreadContext, c as compareMessagesForGrouping } from '../../ThreadProvider-7a96557d.js';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import { isSameDay } from 'date-fns';
import { MessageProvider } from '../../Message/context.js';
import '../../index-0e63045e.js';
import '../../index-26c66412.js';
import '../../LocalizationContext-bcaf447a.js';
import '../../stringSet-7f300d56.js';
import '../../ui/DateSeparator.js';
import '../../color-5dee2e2d.js';
import '../../index-208f60f9.js';
import 'prop-types';
import '../../RemoveMessageModal-3f1727d7.js';
import '../../ui/Modal.js';
import 'react-dom';
import '../../index-5ca598bf.js';
import '../../ui/Button.js';
import '../../ui/Icon.js';
import '../../ui/IconButton.js';
import '../../_rollupPluginBabelHelpers-52628376.js';
import '../../MediaQueryContext-0d6b7936.js';
import '../../ui/FileViewer.js';
import '../../index-46da725e.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../ui/Avatar.js';
import '../../ui/ImageRenderer.js';
import '../../uuid-9e3aeec6.js';
import '../../Channel/components/SuggestedMentionList.js';
import '../../ChannelProvider-205bb2b4.js';
import '../../UserProfileContext-96f68187.js';
import '../../const-4bc8cbc7.js';
import '../../topics-50ed8c29.js';
import '../../compareIds-c483b4db.js';
import '@sendbird/chat/groupChannel';
import '@sendbird/chat/message';
import '../../consts-37c3d0a0.js';
import '../../resolvedReplyType-bfdfe6bd.js';
import '../../const-c64639fc.js';
import '../../withSendbird.js';
import '../context/types.js';
import '@sendbird/chat';
import '../../ui/MessageInput.js';
import 'dompurify';
import '../../consts-acdc6c08.js';
import '../../consts-c99872c0.js';
import '../../consts-cf7328d6.js';
import '../../tokenize-aa91b5a4.js';
import '../../index.module-6fe8c3f8.js';
import '../../ui/ContextMenu.js';
import '../../ui/SortByRow.js';
import '../../ui/UserProfile.js';
import '../../sendbirdSelectors.js';
import '../../utils-4ce9435a.js';
import '../../ui/MessageItemMenu.js';
import '../../types-061bedf1.js';
import '../../ui/MessageItemReactionMenu.js';
import '../../ui/ReactionButton.js';
import '../../useLongPress-8b46567a.js';
import '../../index-4e834e8c.js';
import '../../ui/Loader.js';
import '../../index-fb9203f4.js';
import '../../index-1c8252a9.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 '../../index-78f0d986.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 '../../ui/UnknownMessageItemBody.js';
import '../../ui/VoiceMessageItemBody.js';
import '../../ui/ProgressBar.js';
import '../../VoicePlayer/useVoicePlayer.js';
import '../../index-dea18b86.js';
import '../../VoiceRecorder/context.js';
import '../../ui/PlaybackTime.js';
import '../../index-c216a28a.js';
import '../../useDirtyGetMentions-8a3ab1ab.js';

function ThreadList(_ref) {
  let {
    className,
    allThreadMessages,
    renderMessage,
    renderCustomSeparator,
    scrollRef,
    scrollBottom
  } = _ref;
  const {
    config
  } = useSendbirdStateContext();
  const {
    replyType,
    userId
  } = config;
  const {
    currentChannel
  } = useThreadContext();
  const MemorizedMessage = useMemo(() => _ref2 => {
    let {
      message,
      chainTop,
      chainBottom,
      hasSeparator
    } = _ref2;
    if (typeof renderMessage === 'function') {
      return renderMessage({
        message: message,
        chainTop,
        chainBottom,
        hasSeparator
      });
    }
    return null;
  }, [renderMessage]);
  return /*#__PURE__*/React__default.createElement("div", {
    className: `sendbird-thread-list ${className}`
  }, allThreadMessages.map((message, idx) => {
    var _sender;
    const isByMe = (message === null || message === void 0 ? void 0 : (_sender = message.sender) === null || _sender === void 0 ? void 0 : _sender.userId) === userId;
    const prevMessage = allThreadMessages[idx - 1];
    const nextMessage = allThreadMessages[idx + 1];
    // eslint-disable-next-line no-constant-condition
    const [chainTop, chainBottom] = compareMessagesForGrouping(prevMessage, message, nextMessage, currentChannel, replyType) ;
    const 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));
    const handleScroll = () => {
      const current = scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current;
      if (current) {
        const bottom = current.scrollHeight - current.scrollTop - current.offsetHeight;
        if (scrollBottom < bottom) {
          current.scrollTop += bottom - scrollBottom;
        }
      }
    };
    return /*#__PURE__*/React__default.createElement(MessageProvider, {
      message: message,
      isByMe: isByMe,
      key: message === null || message === void 0 ? void 0 : message.messageId
    }, MemorizedMessage({
      message,
      chainTop,
      chainBottom,
      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
