'use strict';

var React = require('react');
var Thread_components_ThreadListItem = require('./ThreadListItem.js');
var Thread_context = require('../../ThreadProvider-8a541e7e.js');
var useSendbirdStateContext = require('../../useSendbirdStateContext.js');
var dateFns = require('date-fns');
var Message_context = require('../../Message/context.js');
require('../../index-a9099968.js');
require('../../index-c08c7ef8.js');
require('../../LocalizationContext-bc6af64a.js');
require('../../stringSet-812981b5.js');
require('../../ui/DateSeparator.js');
require('../../color-41ee42a1.js');
require('../../index-902525e0.js');
require('prop-types');
require('../../RemoveMessageModal-65e93d87.js');
require('../../ui/Modal.js');
require('react-dom');
require('../../index-09573a50.js');
require('../../ui/Button.js');
require('../../ui/Icon.js');
require('../../ui/IconButton.js');
require('../../_rollupPluginBabelHelpers-00480ba8.js');
require('../../MediaQueryContext-8be571f9.js');
require('../../ui/FileViewer.js');
require('../../index-609a0bf5.js');
require('../../utils/message/getOutgoingMessageState.js');
require('../../ui/Avatar.js');
require('../../ui/ImageRenderer.js');
require('../../uuid-43555bb8.js');
require('../../Channel/components/SuggestedMentionList.js');
require('../../ChannelProvider-e2f422cf.js');
require('../../UserProfileContext-268ad0d1.js');
require('../../const-f1c7b0cf.js');
require('../../topics-623a82c5.js');
require('../../compareIds-a65972c5.js');
require('@sendbird/chat/groupChannel');
require('@sendbird/chat/message');
require('../../consts-d0461d4c.js');
require('../../resolvedReplyType-ab4bb8af.js');
require('../../const-eb6bfc42.js');
require('../../withSendbird.js');
require('../context/types.js');
require('@sendbird/chat');
require('../../ui/MessageInput.js');
require('dompurify');
require('../../consts-c63fe2cd.js');
require('../../consts-db7e33d3.js');
require('../../consts-20c3e20f.js');
require('../../tokenize-846b6f86.js');
require('../../index.module-ceb5d636.js');
require('../../ui/ContextMenu.js');
require('../../ui/SortByRow.js');
require('../../ui/UserProfile.js');
require('../../sendbirdSelectors.js');
require('../../utils-89c82b6d.js');
require('../../ui/MessageItemMenu.js');
require('../../types-d296d350.js');
require('../../ui/MessageItemReactionMenu.js');
require('../../ui/ReactionButton.js');
require('../../useLongPress-d5e59a98.js');
require('../../index-9638a3d3.js');
require('../../ui/Loader.js');
require('../../index-89c089bb.js');
require('../../index-c46b64e3.js');
require('../../ui/EmojiReactions.js');
require('../../ui/ReactionBadge.js');
require('../../ui/BottomSheet.js');
require('../../ui/UserListItem.js');
require('../../ui/MutedAvatarOverlay.js');
require('../../ui/Checkbox.js');
require('../../ui/Tooltip.js');
require('../../ui/TooltipWrapper.js');
require('../../ui/TextMessageItemBody.js');
require('../../index-060f95dc.js');
require('../../ui/MentionLabel.js');
require('../../ui/LinkLabel.js');
require('../../ui/OGMessageItemBody.js');
require('../../ui/FileMessageItemBody.js');
require('../../ui/TextButton.js');
require('../../ui/ThumbnailMessageItemBody.js');
require('../../ui/UnknownMessageItemBody.js');
require('../../ui/VoiceMessageItemBody.js');
require('../../ui/ProgressBar.js');
require('../../VoicePlayer/useVoicePlayer.js');
require('../../index-606b67d2.js');
require('../../VoiceRecorder/context.js');
require('../../ui/PlaybackTime.js');
require('../../index-dd19f542.js');
require('../../useDirtyGetMentions-b4ae7a63.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

function ThreadList(_ref) {
  let {
    className,
    allThreadMessages,
    renderMessage,
    renderCustomSeparator,
    scrollRef,
    scrollBottom
  } = _ref;
  const {
    config
  } = useSendbirdStateContext();
  const {
    replyType,
    userId
  } = config;
  const {
    currentChannel
  } = Thread_context.useThreadContext();
  const MemorizedMessage = React.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["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] = Thread_context.compareMessagesForGrouping(prevMessage, message, nextMessage, currentChannel, replyType) ;
    const hasSeparator = !((prevMessage === null || prevMessage === void 0 ? void 0 : prevMessage.createdAt) > 0 && dateFns.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["default"].createElement(Message_context.MessageProvider, {
      message: message,
      isByMe: isByMe,
      key: message === null || message === void 0 ? void 0 : message.messageId
    }, MemorizedMessage({
      message,
      chainTop,
      chainBottom,
      hasSeparator
    }) || /*#__PURE__*/React__default["default"].createElement(Thread_components_ThreadListItem, {
      message: message,
      chainTop: chainTop,
      chainBottom: chainBottom,
      hasSeparator: hasSeparator,
      renderCustomSeparator: renderCustomSeparator,
      handleScroll: handleScroll
    }));
  }));
}

module.exports = ThreadList;
//# sourceMappingURL=ThreadList.js.map
