import React__default, { useState } from 'react';
import { c as compareMessagesForGrouping, u as useChannelContext, i as isAboutSame } from '../../ChannelProvider-e2c86cba.js';
import { P as PlaceHolder, a as PlaceHolderTypes } from '../../index-e3412d57.js';
import Icon, { IconTypes, IconColors } from '../../ui/Icon.js';
import Message from './Message.js';
import { i as isSameDay } from '../../index-30236e16.js';
import UnreadCount from './UnreadCount.js';
import FrozenNotification from './FrozenNotification.js';
import { M as MESSAGE_SCROLL_BUFFER } from '../../const-26fafb08.js';
import '../../UserProfileContext-d2264917.js';
import 'prop-types';
import '../../useSendbirdStateContext.js';
import '../../withSendbird.js';
import '../../_rollupPluginBabelHelpers-dfc61977.js';
import '../../index-c55389dc.js';
import '../../index-5b34a381.js';
import '../../topics-c4837013.js';
import '../../index-102fbe62.js';
import '../../tslib.es6-645dedf8.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../compareIds-86584ff3.js';
import '@sendbird/chat/groupChannel';
import '../../uuid-e15650df.js';
import '@sendbird/chat/message';
import '../../ui/ContextMenu.js';
import 'react-dom';
import '../../ui/SortByRow.js';
import '../../index-46d8dde5.js';
import '../../stringSet-fd70cba0.js';
import '../../ui/ReactionButton.js';
import '../../ui/ImageRenderer.js';
import '../../consts-37ea645a.js';
import '../../LocalizationContext-187f787e.js';
import '../../ui/Loader.js';
import './SuggestedMentionList.js';
import '../../ui/Avatar.js';
import '../../const-693a408f.js';
import '../../ThreadProvider-8d535c1b.js';
import '../../Thread/context/types.js';
import '@sendbird/chat';
import '../../ui/DateSeparator.js';
import '../../color-b379fbc5.js';
import '../../ui/MessageInput.js';
import '../../ui/IconButton.js';
import '../../ui/Button.js';
import 'dompurify';
import '../../consts-b2395440.js';
import '../../ui/MessageContent.js';
import '../../ui/UserProfile.js';
import '../../sendbirdSelectors.js';
import '../../utils-780c1e12.js';
import '../../index-7b740ffc.js';
import '../../index-7afa5c8d.js';
import '../../ui/MessageItemMenu.js';
import '../../types-87c77099.js';
import '../../ui/MessageItemReactionMenu.js';
import '../../ui/EmojiReactions.js';
import '../../ui/Tooltip.js';
import '../../ui/TooltipWrapper.js';
import '../../ui/ReactionBadge.js';
import '../../ui/AdminMessage.js';
import '../../ui/TextMessageItemBody.js';
import '../../ui/Word.js';
import '../../ui/LinkLabel.js';
import '../../ui/MentionLabel.js';
import '../../ui/FileMessageItemBody.js';
import '../../ui/TextButton.js';
import '../../MediaQueryContext-add671c4.js';
import '../../ui/ThumbnailMessageItemBody.js';
import '../../ui/OGMessageItemBody.js';
import '../../ui/UnknownMessageItemBody.js';
import '../../ui/QuoteMessage.js';
import '../../useLongPress-81323308.js';
import '../../ui/BottomSheet.js';
import '../../index-f338f866.js';
import '../../ui/ThreadReplies.js';
import '../../ui/VoiceMessageItemBody.js';
import '../../ui/ProgressBar.js';
import '../../VoicePlayer/useVoicePlayer.js';
import '../../index-c29a819f.js';
import '../../VoiceRecorder/context.js';
import '../../lame.all.js';
import '../../ui/PlaybackTime.js';
import './FileViewer.js';
import './RemoveMessageModal.js';
import '../../ui/Modal.js';

var getMessagePartsInfo = function (_a) {
  var _b = _a.allMessages,
      allMessages = _b === void 0 ? [] : _b,
      _c = _a.isMessageGroupingEnabled,
      isMessageGroupingEnabled = _c === void 0 ? true : _c,
      _d = _a.currentIndex,
      currentIndex = _d === void 0 ? 0 : _d,
      _e = _a.currentMessage,
      currentMessage = _e === void 0 ? null : _e,
      _f = _a.currentChannel,
      currentChannel = _f === void 0 ? null : _f,
      _g = _a.replyType,
      replyType = _g === void 0 ? '' : _g;
  var previousMessage = allMessages[currentIndex - 1];
  var nextMessage = allMessages[currentIndex + 1];

  var _h = isMessageGroupingEnabled ? compareMessagesForGrouping(previousMessage, currentMessage, nextMessage, currentChannel, replyType) : [false, false],
      chainTop = _h[0],
      chainBottom = _h[1];

  var previousMessageCreatedAt = previousMessage === null || previousMessage === void 0 ? void 0 : previousMessage.createdAt;
  var currentCreatedAt = currentMessage.createdAt; // https://stackoverflow.com/a/41855608

  var hasSeparator = !(previousMessageCreatedAt && isSameDay(currentCreatedAt, previousMessageCreatedAt));
  return {
    chainTop: chainTop,
    chainBottom: chainBottom,
    hasSeparator: hasSeparator
  };
};

var SCROLL_REF_CLASS_NAME = '.sendbird-msg--scroll-ref';

var MessageList = function (_a) {
  var _b = _a.className,
      className = _b === void 0 ? '' : _b,
      renderMessage = _a.renderMessage,
      renderPlaceholderEmpty = _a.renderPlaceholderEmpty,
      renderCustomSeparator = _a.renderCustomSeparator,
      renderPlaceholderLoader = _a.renderPlaceholderLoader;

  var _c = useChannelContext(),
      allMessages = _c.allMessages,
      hasMorePrev = _c.hasMorePrev,
      setInitialTimeStamp = _c.setInitialTimeStamp,
      setAnimatedMessageId = _c.setAnimatedMessageId,
      setHighLightedMessageId = _c.setHighLightedMessageId,
      isMessageGroupingEnabled = _c.isMessageGroupingEnabled,
      scrollRef = _c.scrollRef,
      onScrollCallback = _c.onScrollCallback,
      onScrollDownCallback = _c.onScrollDownCallback,
      messagesDispatcher = _c.messagesDispatcher,
      messageActionTypes = _c.messageActionTypes,
      currentGroupChannel = _c.currentGroupChannel,
      disableMarkAsRead = _c.disableMarkAsRead,
      replyType = _c.replyType,
      loading = _c.loading,
      unreadSince = _c.unreadSince;

  var _d = useState(0),
      scrollBottom = _d[0],
      setScrollBottom = _d[1];

  var onScroll = function (e) {
    var element = e.target;
    var scrollTop = element.scrollTop,
        clientHeight = element.clientHeight,
        scrollHeight = element.scrollHeight;

    if (scrollTop === 0) {
      if (!hasMorePrev) {
        return;
      }

      var nodes = scrollRef.current.querySelectorAll(SCROLL_REF_CLASS_NAME);
      var first_1 = nodes && nodes[0];
      onScrollCallback(function (_a) {
        var messages = _a[0];

        if (messages) {
          // https://github.com/scabbiaza/react-scroll-position-on-updating-dom
          // Set block to nearest to prevent unexpected scrolling from outer components
          try {
            first_1.scrollIntoView({
              block: "start",
              inline: "nearest"
            });
          } catch (error) {//
          }
        }
      });
    }

    if (isAboutSame(clientHeight + scrollTop, scrollHeight, MESSAGE_SCROLL_BUFFER)) {
      onScrollDownCallback(function (_a) {
        _a[0];
      });
    } // Save the lastest scroll bottom value


    if (scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current) {
      var current = scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current;
      setScrollBottom(current.scrollHeight - current.scrollTop - current.offsetHeight);
    }

    if (!disableMarkAsRead && isAboutSame(clientHeight + scrollTop, scrollHeight, MESSAGE_SCROLL_BUFFER)) {
      // Mark as read if scroll is at end
      setTimeout(function () {
        var _a;

        messagesDispatcher({
          type: messageActionTypes.MARK_AS_READ,
          payload: {
            channel: currentGroupChannel
          }
        });

        try {
          (_a = currentGroupChannel === null || currentGroupChannel === void 0 ? void 0 : currentGroupChannel.markAsRead) === null || _a === void 0 ? void 0 : _a.call(currentGroupChannel);
        } catch (_b) {//
        }
      }, 500);
    }
  };

  var onClickScrollBot = function () {
    var _a, _b, _c;

    setInitialTimeStamp === null || setInitialTimeStamp === void 0 ? void 0 : setInitialTimeStamp(null);
    setAnimatedMessageId === null || setAnimatedMessageId === void 0 ? void 0 : setAnimatedMessageId(null);
    setHighLightedMessageId === null || setHighLightedMessageId === void 0 ? void 0 : setHighLightedMessageId(null);

    if (((_a = scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current) === null || _a === void 0 ? void 0 : _a.scrollTop) > -1) {
      scrollRef.current.scrollTop = ((_b = scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current) === null || _b === void 0 ? void 0 : _b.scrollHeight) - ((_c = scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current) === null || _c === void 0 ? void 0 : _c.offsetHeight);
    }
  };

  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 && scrollBottom <= MESSAGE_SCROLL_BUFFER) {
        current.scrollTop += bottom - scrollBottom;
      }
    }
  };

  if (loading) {
    return typeof renderPlaceholderLoader === 'function' ? renderPlaceholderLoader() : /*#__PURE__*/React__default.createElement(PlaceHolder, {
      type: PlaceHolderTypes.LOADING
    });
  }

  if (allMessages.length < 1) {
    if (renderPlaceholderEmpty && typeof renderPlaceholderEmpty === 'function') {
      return renderPlaceholderEmpty();
    }

    return /*#__PURE__*/React__default.createElement(PlaceHolder, {
      className: "sendbird-conversation__no-messages",
      type: PlaceHolderTypes.NO_MESSAGES
    });
  }

  return /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-conversation__messages ".concat(className)
  }, /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-conversation__scroll-container"
  }, /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-conversation__padding"
  }), /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-conversation__messages-padding",
    ref: scrollRef,
    onScroll: onScroll
  }, allMessages.map(function (m, idx) {
    var _a = getMessagePartsInfo({
      allMessages: allMessages,
      replyType: replyType,
      isMessageGroupingEnabled: isMessageGroupingEnabled,
      currentIndex: idx,
      currentMessage: m,
      currentChannel: currentGroupChannel
    }),
        chainTop = _a.chainTop,
        chainBottom = _a.chainBottom,
        hasSeparator = _a.hasSeparator;

    return /*#__PURE__*/React__default.createElement(Message, {
      key: m === null || m === void 0 ? void 0 : m.messageId,
      handleScroll: handleScroll,
      renderMessage: renderMessage,
      message: m,
      hasSeparator: hasSeparator,
      chainTop: chainTop,
      chainBottom: chainBottom,
      renderCustomSeparator: renderCustomSeparator
    });
  }))), (currentGroupChannel === null || currentGroupChannel === void 0 ? void 0 : currentGroupChannel.isFrozen) && /*#__PURE__*/React__default.createElement(FrozenNotification, {
    className: "sendbird-conversation__messages__notification"
  }), /*#__PURE__*/React__default.createElement(UnreadCount, {
    className: "sendbird-conversation__messages__notification",
    count: currentGroupChannel === null || currentGroupChannel === void 0 ? void 0 : currentGroupChannel.unreadMessageCount,
    time: unreadSince,
    onClick: function () {
      var _a, _b, _c;

      if ((_a = scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current) === null || _a === void 0 ? void 0 : _a.scrollTop) {
        scrollRef.current.scrollTop = ((_b = scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current) === null || _b === void 0 ? void 0 : _b.scrollHeight) - ((_c = scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current) === null || _c === void 0 ? void 0 : _c.offsetHeight);
      }

      if (!disableMarkAsRead) {
        try {
          currentGroupChannel === null || currentGroupChannel === void 0 ? void 0 : currentGroupChannel.markAsRead();
        } catch (_d) {//
        }

        messagesDispatcher({
          type: messageActionTypes.MARK_AS_READ,
          payload: {
            channel: currentGroupChannel
          }
        });
      }

      setInitialTimeStamp(null);
      setAnimatedMessageId(null);
      setHighLightedMessageId(null);
    }
  }), // This flag is an unmatched variable
  scrollBottom > 1 && /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-conversation__scroll-bottom-button",
    onClick: onClickScrollBot,
    onKeyDown: onClickScrollBot,
    tabIndex: 0,
    role: "button"
  }, /*#__PURE__*/React__default.createElement(Icon, {
    width: "24px",
    height: "24px",
    type: IconTypes.CHEVRON_DOWN,
    fillColor: IconColors.PRIMARY
  })));
};

export { MessageList as default };
//# sourceMappingURL=MessageList.js.map
