import React__default, { useRef, useState, useMemo } from 'react';
import { i as isSameDay } from '../../index-3f8e1a51.js';
import Icon, { IconTypes, IconColors } from '../../ui/Icon.js';
import { P as PlaceHolder, a as PlaceHolderTypes } from '../../index-11fe17e1.js';
import { u as useOpenChannelContext, c as compareMessagesForGrouping } from '../../OpenChannelProvider-995a7397.js';
import MessagOpenChannelMessageeHoc from './OpenChannelMessage.js';
import { MessageProvider } from '../../Message/context.js';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import { a as useHandleOnScrollCallback } from '../../index-3461216d.js';
import '../../index-5a62f6b7.js';
import '../../index-ff8f2f61.js';
import 'prop-types';
import '../../LocalizationContext-1701e5c8.js';
import '../../stringSet-e9bb0333.js';
import '../../index-f50d7ab0.js';
import '../../ui/Loader.js';
import '../../UserProfileContext-74e9a128.js';
import '../../_rollupPluginBabelHelpers-cc035350.js';
import '../../compareIds-367bb030.js';
import '../../topics-0b30f52b.js';
import '@sendbird/chat';
import '@sendbird/chat/openChannel';
import '../../uuid-8b344dd2.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-99ab01f6.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../ui/IconButton.js';
import '../../ui/UserProfile.js';
import '../../sendbirdSelectors.js';
import '../../utils-05aa9060.js';
import '../../ui/Button.js';
import '../../utils-a59107a2.js';
import '../../index-e30c58e0.js';
import '../../MediaQueryContext-e5b401d0.js';
import '../../useLongPress-6d0b6619.js';
import '../../ui/OpenChannelAdminMessage.js';
import '../../ui/OpenchannelOGMessage.js';
import '../../ui/LinkLabel.js';
import '../../index-a31ee68d.js';
import '../../index.module-d24badee.js';
import '../../tokenize-d12690c3.js';
import '../../ui/MentionLabel.js';
import '../../ui/OpenchannelThumbnailMessage.js';
import '../../ui/OpenchannelFileMessage.js';
import '../../ui/TextButton.js';
import '../../color-ea3bf5b8.js';
import '../../ui/DateSeparator.js';
import '../../ui/MessageInput.js';
import '../../const-2e67461b.js';
import '../../const-7c266a95.js';
import 'dompurify';
import '../../consts-a0834b70.js';
import '../../consts-3a33a0a8.js';
import '../../consts-b3274b3d.js';
import '../../ui/FileViewer.js';
import '../../index-44c86e58.js';
import '../../ui/Modal.js';
import '../../withSendbird.js';
import '../../consts-44eaef6b.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
