import React__default, { useRef, useState, useMemo } from 'react';
import { i as isSameDay } from '../../index-1c8252a9.js';
import Icon, { IconTypes, IconColors } from '../../ui/Icon.js';
import { P as PlaceHolder, a as PlaceHolderTypes } from '../../index-b26a2bcd.js';
import { u as useOpenChannelContext, c as compareMessagesForGrouping } from '../../OpenChannelProvider-770c207c.js';
import MessagOpenChannelMessageeHoc from './OpenChannelMessage.js';
import { MessageProvider } from '../../Message/context.js';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import { a as useHandleOnScrollCallback } from '../../index-93228741.js';
import '../../index-0e63045e.js';
import '../../index-26c66412.js';
import 'prop-types';
import '../../LocalizationContext-bcaf447a.js';
import '../../stringSet-7f300d56.js';
import '../../index-208f60f9.js';
import '../../ui/Loader.js';
import '../../UserProfileContext-96f68187.js';
import '../../_rollupPluginBabelHelpers-52628376.js';
import '../../compareIds-c483b4db.js';
import '../../topics-50ed8c29.js';
import '@sendbird/chat';
import '@sendbird/chat/openChannel';
import '../../uuid-9e3aeec6.js';
import '../../ui/OpenchannelUserMessage.js';
import '../../ui/Avatar.js';
import '../../ui/ImageRenderer.js';
import '../../ui/ContextMenu.js';
import 'react-dom';
import '../../ui/SortByRow.js';
import '../../index-46da725e.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../ui/IconButton.js';
import '../../ui/UserProfile.js';
import '../../sendbirdSelectors.js';
import '../../utils-4ce9435a.js';
import '../../ui/Button.js';
import '../../utils-8e981411.js';
import '../../index-c4826ede.js';
import '../../MediaQueryContext-0d6b7936.js';
import '../../useLongPress-8b46567a.js';
import '../../ui/OpenChannelAdminMessage.js';
import '../../ui/OpenchannelOGMessage.js';
import '../../ui/LinkLabel.js';
import '../../index-78f0d986.js';
import '../../index.module-6fe8c3f8.js';
import '../../tokenize-aa91b5a4.js';
import '../../ui/MentionLabel.js';
import '../../ui/OpenchannelThumbnailMessage.js';
import '../../ui/OpenchannelFileMessage.js';
import '../../ui/TextButton.js';
import '../../color-5dee2e2d.js';
import '../../ui/DateSeparator.js';
import '../../ui/MessageInput.js';
import '../../const-c64639fc.js';
import '../../const-4bc8cbc7.js';
import 'dompurify';
import '../../consts-acdc6c08.js';
import '../../consts-c99872c0.js';
import '../../consts-cf7328d6.js';
import '../../ui/FileViewer.js';
import '../../index-5ca598bf.js';
import '../../ui/Modal.js';
import '../../withSendbird.js';
import '../../consts-37c3d0a0.js';

function OpenchannelMessageList(props, ref) {
  const {
    isMessageGroupingEnabled = true,
    allMessages,
    hasMore,
    onScroll
  } = useOpenChannelContext();
  const store = useSendbirdStateContext();
  const userId = store.config.userId;
  const scrollRef = ref || useRef(null);
  const [showScrollDownButton, setShowScrollDownButton] = useState(false);
  const scrollToBottom = () => {
    if (scrollRef && scrollRef.current) {
      scrollRef.current.scrollTo(0, scrollRef.current.scrollHeight);
      setShowScrollDownButton(false);
    }
  };
  const handleOnScroll = useHandleOnScrollCallback({
    setShowScrollDownButton,
    hasMore,
    onScroll,
    scrollRef
  });
  const memoizedMessageList = 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 && isSameDay(currentCreatedAt, previousMessageCreatedAt));
        const [chainTop, chainBottom] = isMessageGroupingEnabled ? 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.createElement(MessageProvider, {
          message: message,
          isByMe: isByMe,
          key: key
        }, /*#__PURE__*/React__default.createElement(MessagOpenChannelMessageeHoc, {
          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.createElement(PlaceHolder, {
      className: "sendbird-openchannel-conversation-scroll__container__place-holder",
      type: PlaceHolderTypes.NO_MESSAGES
    });
  }, [allMessages]);
  return /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-conversation-scroll"
  }, /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-conversation-scroll__container"
  }, /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-conversation-scroll__container__padding"
  }), /*#__PURE__*/React__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.createElement("div", {
    className: "sendbird-openchannel-conversation-scroll__container__scroll-bottom-button",
    onClick: scrollToBottom,
    onKeyDown: scrollToBottom,
    tabIndex: 0,
    role: "button"
  }, /*#__PURE__*/React__default.createElement(Icon, {
    width: "24px",
    height: "24px",
    type: IconTypes.CHEVRON_DOWN,
    fillColor: IconColors.CONTENT
  })));
}
var OpenChannelMessageList = /*#__PURE__*/React__default.forwardRef(OpenchannelMessageList);

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