import React__default, { useMemo } from 'react';
import ThreadListItem from './ThreadListItem.js';
import { u as useThreadContext, c as compareMessagesForGrouping } from '../../ThreadProvider-7f67575f.js';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import { isSameDay } from 'date-fns';
import { MessageProvider } from '../../Message/context.js';
import '../../index-e7f4cbdf.js';
import '../../index-90420b99.js';
import '../../LocalizationContext-426f3d58.js';
import '../../stringSet-a62108b2.js';
import '../../ui/DateSeparator.js';
import '../../color-e708f53d.js';
import '../../index-4105bc4d.js';
import 'prop-types';
import '../../RemoveMessageModal-3d9ef84c.js';
import '../../ui/Modal.js';
import 'react-dom';
import '../../index-3e548496.js';
import '../../ui/Button.js';
import '../../ui/Icon.js';
import '../../ui/IconButton.js';
import '../../_rollupPluginBabelHelpers-a69fd1ce.js';
import '../../MediaQueryContext-b531adc9.js';
import '../../ui/FileViewer.js';
import '../../index-f83f0aac.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../ui/Avatar.js';
import '../../ui/ImageRenderer.js';
import '../../uuid-2e8efc75.js';
import '../../Channel/components/SuggestedMentionList.js';
import '../../ChannelProvider-3bd92d16.js';
import '../../UserProfileContext-a59f0533.js';
import '../../const-9982664a.js';
import '../../topics-247fcf3a.js';
import '../../compareIds-aca19765.js';
import '@sendbird/chat/groupChannel';
import '@sendbird/chat/message';
import '../../consts-e64103c6.js';
import '../../resolvedReplyType-7bde5f56.js';
import '../../const-ddd9d461.js';
import '../../withSendbird.js';
import '../context/types.js';
import '@sendbird/chat';
import '../../ui/MessageInput.js';
import 'dompurify';
import '../../consts-15e764c0.js';
import '../../consts-f99cd0da.js';
import '../../consts-2eeaa3de.js';
import '../../tokenize-eec42500.js';
import '../../index.module-8bb7760e.js';
import '../../ui/ContextMenu.js';
import '../../ui/SortByRow.js';
import '../../ui/UserProfile.js';
import '../../sendbirdSelectors.js';
import '../../utils-4bf6e4e6.js';
import '../../ui/MessageItemMenu.js';
import '../../types-d72e53f1.js';
import '../../ui/MessageItemReactionMenu.js';
import '../../ui/ReactionButton.js';
import '../../useLongPress-c0ba590c.js';
import '../../index-a9ca43b0.js';
import '../../ui/Loader.js';
import '../../index-65ed45e9.js';
import '../../index-e40d8b24.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-89c84c27.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-52ba73b9.js';
import '../../VoiceRecorder/context.js';
import '../../ui/PlaybackTime.js';
import '../../index-eefc5b10.js';
import '../../useDirtyGetMentions-f302bddb.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
