'use strict';

var React = require('react');
var Thread_components_ThreadListItem = require('./ThreadListItem.js');
var Thread_context = require('../../ThreadProvider-5eafb9ef.js');
var useSendbirdStateContext = require('../../useSendbirdStateContext.js');
var dateFns = require('date-fns');
require('../../tslib.es6-d2ba1a6b.js');
require('../../index-81e19961.js');
require('../../index-f5502150.js');
require('../../LocalizationContext-1f669b3c.js');
require('../../stringSet-ddd351f0.js');
require('../../ui/DateSeparator.js');
require('../../color-97cfc61b.js');
require('../../index-1b11bb85.js');
require('prop-types');
require('../../RemoveMessageModal-b0c539a1.js');
require('../../ui/Modal.js');
require('react-dom');
require('../../index-577c9a9b.js');
require('../../ui/Button.js');
require('../../ui/Icon.js');
require('../../ui/IconButton.js');
require('../../MediaQueryContext-130143d7.js');
require('../../ui/FileViewer.js');
require('../../index-b32fce67.js');
require('../../utils/message/getOutgoingMessageState.js');
require('../../ui/Avatar.js');
require('../../ui/ImageRenderer.js');
require('../../uuid-28e4a814.js');
require('../../Channel/components/SuggestedMentionList.js');
require('../../ChannelProvider-b1290e55.js');
require('../../UserProfileContext-cde95db8.js');
require('../../topics-5edbb1b2.js');
require('../../_rollupPluginBabelHelpers-32557b21.js');
require('../../compareIds-32f97a73.js');
require('../../const-77194948.js');
require('@sendbird/chat/groupChannel');
require('@sendbird/chat/message');
require('../../ui/ContextMenu.js');
require('../../ui/SortByRow.js');
require('../../ui/ReactionButton.js');
require('../../consts-f82aaae9.js');
require('../../const-fe838b59.js');
require('../../withSendbird.js');
require('../context/types.js');
require('@sendbird/chat');
require('../../ui/MessageInput.js');
require('react-dom/server');
require('../../ui/MentionUserLabel.js');
require('dompurify');
require('../../ui/UserProfile.js');
require('../../sendbirdSelectors.js');
require('../../utils-602ab2fc.js');
require('../../ui/MessageItemMenu.js');
require('../../types-3fb94919.js');
require('../../ui/MessageItemReactionMenu.js');
require('../../index-b911f8b3.js');
require('../../ui/Loader.js');
require('../../index-c4869cdb.js');
require('../../index-78e2bb42.js');
require('../../ui/EmojiReactions.js');
require('../../ui/Tooltip.js');
require('../../ui/TooltipWrapper.js');
require('../../ui/ReactionBadge.js');
require('../../ui/TextMessageItemBody.js');
require('../../ui/Word.js');
require('../../ui/LinkLabel.js');
require('../../ui/MentionLabel.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-26ff28a8.js');
require('../../VoiceRecorder/context.js');
require('../../lame.all.js');
require('../../ui/PlaybackTime.js');

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

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

function ThreadList(_a) {
  var className = _a.className,
      allThreadMessages = _a.allThreadMessages,
      renderMessage = _a.renderMessage,
      renderCustomSeparator = _a.renderCustomSeparator,
      scrollRef = _a.scrollRef,
      scrollBottom = _a.scrollBottom;
  var config = useSendbirdStateContext().config;
  var replyType = config.replyType;
  var currentChannel = Thread_context.useThreadContext().currentChannel;
  var MemorizedMessage = React.useMemo(function () {
    return function (_a) {
      var message = _a.message,
          chainTop = _a.chainTop,
          chainBottom = _a.chainBottom,
          hasSeparator = _a.hasSeparator;

      if (typeof renderMessage === 'function') {
        return renderMessage({
          message: message,
          chainTop: chainTop,
          chainBottom: chainBottom,
          hasSeparator: hasSeparator
        });
      }

      return null;
    };
  }, [renderMessage]);
  return /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-thread-list ".concat(className)
  }, allThreadMessages.map(function (message, idx) {
    var prevMessage = allThreadMessages[idx - 1];
    var nextMessage = allThreadMessages[idx + 1];

    var _a = Thread_context.compareMessagesForGrouping(prevMessage, message, nextMessage, currentChannel, replyType) ,
        chainTop = _a[0],
        chainBottom = _a[1];

    var 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));

    var handleScroll = function () {
      var current = scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current;

      if (current) {
        var bottom = current.scrollHeight - current.scrollTop - current.offsetHeight;

        if (scrollBottom < bottom) {
          current.scrollTop += bottom - scrollBottom;
        }
      }
    };

    return MemorizedMessage({
      message: message,
      chainTop: chainTop,
      chainBottom: chainBottom,
      hasSeparator: 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
