'use strict';

var React = require('react');
var Thread_components_ThreadListItem = require('./ThreadListItem.js');
var Thread_context = require('../../ThreadProvider-8ba81f66.js');
var useSendbirdStateContext = require('../../useSendbirdStateContext.js');
var dateFns = require('date-fns');
require('../../tslib.es6-259cb5ca.js');
require('../../index-5bc31321.js');
require('../../index-6ca5c151.js');
require('../../LocalizationContext-9a1e0d36.js');
require('../../stringSet-2696d00d.js');
require('../../ui/DateSeparator.js');
require('../../color-4b850c2c.js');
require('../../index-d714b895.js');
require('prop-types');
require('../../RemoveMessageModal-b64c35a1.js');
require('../../ui/Modal.js');
require('react-dom');
require('../../index-3d069920.js');
require('../../ui/Button.js');
require('../../ui/Icon.js');
require('../../ui/IconButton.js');
require('../../MediaQueryContext-7f516d81.js');
require('../../ui/FileViewer.js');
require('../../index-b5c769e1.js');
require('../../utils/message/getOutgoingMessageState.js');
require('../../ui/Avatar.js');
require('../../ui/ImageRenderer.js');
require('../../uuid-4e015383.js');
require('../../Channel/components/SuggestedMentionList.js');
require('../../ChannelProvider-a765f8a9.js');
require('../../UserProfileContext-2648d24c.js');
require('../../topics-528fde56.js');
require('../../_rollupPluginBabelHelpers-ada5a2bb.js');
require('../../compareIds-03d1c5fd.js');
require('../../const-cd2f7184.js');
require('@sendbird/chat/groupChannel');
require('@sendbird/chat/message');
require('../../ui/ContextMenu.js');
require('../../ui/SortByRow.js');
require('../../ui/ReactionButton.js');
require('../../const-5080a6f8.js');
require('../../withSendbird.js');
require('../context/types.js');
require('@sendbird/chat');
require('../../ui/MessageInput.js');
require('react-dom/server');
require('../../ui/MentionUserLabel.js');
require('../../ui/UserProfile.js');
require('../../sendbirdSelectors.js');
require('../../utils-afb14420.js');
require('../../ui/MessageItemMenu.js');
require('../../ui/MessageItemReactionMenu.js');
require('../../ui/MessageStatus.js');
require('../../ui/Loader.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');

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
