'use strict';

var React = require('react');
var index$1 = require('../../index-afada680.js');
var ui_Icon = require('../../ui/Icon.js');
var ui_PlaceHolder = require('../../index-9d9e563e.js');
var OpenChannel_context = require('../../OpenChannelProvider-94eb45b4.js');
var OpenChannel_components_OpenChannelMessage = require('./OpenChannelMessage.js');
var Message_context = require('../../Message/context.js');
var useSendbirdStateContext = require('../../useSendbirdStateContext.js');
var index = require('../../index-c38404d2.js');
require('../../index-849f44b0.js');
require('../../index-97005534.js');
require('prop-types');
require('../../LocalizationContext-b42fa07d.js');
require('../../stringSet-4f30313f.js');
require('../../index-72e412f7.js');
require('../../ui/Loader.js');
require('../../UserProfileContext-c87514d3.js');
require('../../_rollupPluginBabelHelpers-3a9d1641.js');
require('../../compareIds-a7029d6a.js');
require('../../topics-3226c6ee.js');
require('@sendbird/chat');
require('@sendbird/chat/openChannel');
require('../../uuid-5ab4f88b.js');
require('../../ui/OpenchannelUserMessage.js');
require('../../ui/Avatar.js');
require('../../ui/ImageRenderer.js');
require('../../ui/ContextMenu.js');
require('react-dom');
require('../../ui/SortByRow.js');
require('../../index-0b8a1074.js');
require('../../utils/message/getOutgoingMessageState.js');
require('../../ui/IconButton.js');
require('../../ui/UserProfile.js');
require('../../sendbirdSelectors.js');
require('../../utils-146517a9.js');
require('../../ui/Button.js');
require('../../utils-dbaff0d0.js');
require('../../index-f4f6d794.js');
require('../../MediaQueryContext-372b55a6.js');
require('../../useLongPress-bf1e8eee.js');
require('../../ui/OpenChannelAdminMessage.js');
require('../../ui/OpenchannelOGMessage.js');
require('../../ui/LinkLabel.js');
require('../../index-1c18afa8.js');
require('../../index.module-d2876b1c.js');
require('../../tokenize-e944d548.js');
require('../../ui/MentionLabel.js');
require('../../ui/OpenchannelThumbnailMessage.js');
require('../../ui/OpenchannelFileMessage.js');
require('../../ui/TextButton.js');
require('../../color-a4e73e6f.js');
require('../../ui/DateSeparator.js');
require('../../ui/MessageInput.js');
require('../../const-3ab3e45c.js');
require('../../const-68cf2709.js');
require('dompurify');
require('../../consts-bb2a67c7.js');
require('../../consts-ad8ba629.js');
require('../../consts-26d4668c.js');
require('../../ui/FileViewer.js');
require('../../index-e8b83547.js');
require('../../ui/Modal.js');
require('../../withSendbird.js');
require('../../consts-f71467c8.js');

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

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

function OpenchannelMessageList(props, ref) {
  const {
    isMessageGroupingEnabled = true,
    allMessages,
    hasMore,
    onScroll
  } = OpenChannel_context.useOpenChannelContext();
  const store = useSendbirdStateContext();
  const userId = store.config.userId;
  const scrollRef = ref || React.useRef(null);
  const [showScrollDownButton, setShowScrollDownButton] = React.useState(false);
  const scrollToBottom = () => {
    if (scrollRef && scrollRef.current) {
      scrollRef.current.scrollTo(0, scrollRef.current.scrollHeight);
      setShowScrollDownButton(false);
    }
  };
  const handleOnScroll = index.useHandleOnScrollCallback({
    setShowScrollDownButton,
    hasMore,
    onScroll,
    scrollRef
  });
  const memoizedMessageList = React.useMemo(() => {
    var _props$renderPlaceHol;
    if (allMessages.length > 0) {
      return allMessages.map((message, index) => {
        var _sender;
        const previousMessage = allMessages[index - 1];
        const nextMessage = allMessages[index - 1];
        const previousMessageCreatedAt = previousMessage && previousMessage.createdAt;
        const currentCreatedAt = message === null || message === void 0 ? void 0 : message.createdAt;
        // https://stackoverflow.com/a/41855608
        const hasSeparator = !(previousMessageCreatedAt && index$1.isSameDay(currentCreatedAt, previousMessageCreatedAt));
        const [chainTop, chainBottom] = isMessageGroupingEnabled ? OpenChannel_context.compareMessagesForGrouping(previousMessage, message, nextMessage) : [false, false];
        const isByMe = (message === null || message === void 0 ? void 0 : (_sender = message.sender) === null || _sender === void 0 ? void 0 : _sender.userId) === userId;
        const key = (message === null || message === void 0 ? void 0 : message.messageId) || (message === null || message === void 0 ? void 0 : message.reqId);
        return /*#__PURE__*/React__default["default"].createElement(Message_context.MessageProvider, {
          message: message,
          isByMe: isByMe,
          key: key
        }, /*#__PURE__*/React__default["default"].createElement(OpenChannel_components_OpenChannelMessage, {
          message: message,
          chainTop: chainTop,
          chainBottom: chainBottom,
          hasSeparator: hasSeparator,
          renderMessage: props === null || props === void 0 ? void 0 : props.renderMessage
        }));
      });
    }
    return (props === null || props === void 0 ? void 0 : (_props$renderPlaceHol = props.renderPlaceHolderEmptyList) === null || _props$renderPlaceHol === void 0 ? void 0 : _props$renderPlaceHol.call(props)) || /*#__PURE__*/React__default["default"].createElement(ui_PlaceHolder.PlaceHolder, {
      className: "sendbird-openchannel-conversation-scroll__container__place-holder",
      type: ui_PlaceHolder.PlaceHolderTypes.NO_MESSAGES
    });
  }, [allMessages]);
  return /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-conversation-scroll"
  }, /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-conversation-scroll__container"
  }, /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-conversation-scroll__container__padding"
  }), /*#__PURE__*/React__default["default"].createElement("div", {
    className: ['sendbird-openchannel-conversation-scroll__container__item-container', allMessages.length > 0 ? '' : 'no-messages'].join(' '),
    onScroll: handleOnScroll,
    ref: scrollRef
  }, memoizedMessageList)), showScrollDownButton && /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-conversation-scroll__container__scroll-bottom-button",
    onClick: scrollToBottom,
    onKeyDown: scrollToBottom,
    tabIndex: 0,
    role: "button"
  }, /*#__PURE__*/React__default["default"].createElement(ui_Icon["default"], {
    width: "24px",
    height: "24px",
    type: ui_Icon.IconTypes.CHEVRON_DOWN,
    fillColor: ui_Icon.IconColors.CONTENT
  })));
}
var OpenChannelMessageList = /*#__PURE__*/React__default["default"].forwardRef(OpenchannelMessageList);

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