import React__default, { useMemo } from 'react';
import ThreadListItem from './ThreadListItem.js';
import { u as useThreadContext, c as compareMessagesForGrouping } from '../../ThreadProvider-dea308a0.js';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import { isSameDay } from 'date-fns';
import { MessageProvider } from '../../Message/context.js';
import '../../index-5a62f6b7.js';
import '../../index-ff8f2f61.js';
import '../../LocalizationContext-1701e5c8.js';
import '../../stringSet-e9bb0333.js';
import '../../ui/DateSeparator.js';
import '../../color-ea3bf5b8.js';
import '../../index-f50d7ab0.js';
import 'prop-types';
import '../../RemoveMessageModal-bf0ab3b2.js';
import '../../ui/Modal.js';
import 'react-dom';
import '../../index-44c86e58.js';
import '../../ui/Button.js';
import '../../ui/Icon.js';
import '../../ui/IconButton.js';
import '../../_rollupPluginBabelHelpers-cc035350.js';
import '../../MediaQueryContext-e5b401d0.js';
import '../../ui/FileViewer.js';
import '../../index-99ab01f6.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../ui/Avatar.js';
import '../../ui/ImageRenderer.js';
import '../../uuid-8b344dd2.js';
import '../../Channel/components/SuggestedMentionList.js';
import '../../ChannelProvider-1dc36682.js';
import '../../UserProfileContext-74e9a128.js';
import '../../const-7c266a95.js';
import '../../topics-0b30f52b.js';
import '../../compareIds-367bb030.js';
import '@sendbird/chat/groupChannel';
import '@sendbird/chat/message';
import '../../consts-44eaef6b.js';
import '../../resolvedReplyType-6bb8f548.js';
import '../../const-2e67461b.js';
import '../../withSendbird.js';
import '../context/types.js';
import '@sendbird/chat';
import '../../ui/MessageInput.js';
import 'dompurify';
import '../../consts-a0834b70.js';
import '../../consts-3a33a0a8.js';
import '../../consts-b3274b3d.js';
import '../../tokenize-d12690c3.js';
import '../../index.module-d24badee.js';
import '../../ui/ContextMenu.js';
import '../../ui/SortByRow.js';
import '../../ui/UserProfile.js';
import '../../sendbirdSelectors.js';
import '../../utils-05aa9060.js';
import '../../ui/MessageItemMenu.js';
import '../../types-28e84810.js';
import '../../ui/MessageItemReactionMenu.js';
import '../../ui/ReactionButton.js';
import '../../useLongPress-6d0b6619.js';
import '../../index-0aa8cbad.js';
import '../../ui/Loader.js';
import '../../index-7e5538e6.js';
import '../../index-3f8e1a51.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-a31ee68d.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-02ceed0a.js';
import '../../VoiceRecorder/context.js';
import '../../ui/PlaybackTime.js';
import '../../index-35696958.js';
import '../../useDirtyGetMentions-2df51ec3.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
