import React__default, { useMemo } from 'react';
import ThreadListItem from './ThreadListItem.js';
import { u as useThreadContext, c as compareMessagesForGrouping } from '../../ThreadProvider-cf357120.js';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import { isSameDay } from 'date-fns';
import { MessageProvider } from '../../Message/context.js';
import '../../index-ccef08fe.js';
import '../../index-6add7940.js';
import '../../LocalizationContext-a82461b5.js';
import '../../stringSet-b60e9529.js';
import '../../ui/DateSeparator.js';
import '../../color-91e43dce.js';
import '../../index-6420013c.js';
import 'prop-types';
import '../../RemoveMessageModal-2adc29ce.js';
import '../../ui/Modal.js';
import 'react-dom';
import '../../index-a9101009.js';
import '../../ui/Button.js';
import '../../ui/Icon.js';
import '../../ui/IconButton.js';
import '../../_rollupPluginBabelHelpers-05128ded.js';
import '../../MediaQueryContext-d6110905.js';
import '../../ui/FileViewer.js';
import '../../index-579ca9d7.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../ui/Avatar.js';
import '../../ui/ImageRenderer.js';
import '../../uuid-717930b4.js';
import '../../Channel/components/SuggestedMentionList.js';
import '../../ChannelProvider-5dcbee7b.js';
import '../../UserProfileContext-f3f446d2.js';
import '../../const-72f012b6.js';
import '../../topics-eb548880.js';
import '../../compareIds-2e50e905.js';
import '@sendbird/chat/groupChannel';
import '@sendbird/chat/message';
import '../../consts-fa6ab3c0.js';
import '../../resolvedReplyType-9d00a3db.js';
import '../../const-3a34a127.js';
import '../../withSendbird.js';
import '../context/types.js';
import '@sendbird/chat';
import '../../ui/MessageInput.js';
import 'dompurify';
import '../../consts-b4cb83cf.js';
import '../../consts-7693b8d3.js';
import '../../consts-9469111b.js';
import '../../tokenize-c718324d.js';
import '../../index.module-4f1cd64e.js';
import '../../ui/ContextMenu.js';
import '../../ui/SortByRow.js';
import '../../ui/UserProfile.js';
import '../../sendbirdSelectors.js';
import '../../utils-e0c7329c.js';
import '../../ui/MessageItemMenu.js';
import '../../types-95cfd227.js';
import '../../ui/MessageItemReactionMenu.js';
import '../../ui/ReactionButton.js';
import '../../useLongPress-12280c78.js';
import '../../index-ac0b2d20.js';
import '../../ui/Loader.js';
import '../../index-f879f6ef.js';
import '../../index-ad50e2c0.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-a2bb2783.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-fa5b2b8c.js';
import '../../VoiceRecorder/context.js';
import '../../ui/PlaybackTime.js';
import '../../index-86294a68.js';
import '../../useDirtyGetMentions-dffd4046.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
