import React__default, { useRef, useState, useMemo } from 'react';
import { i as isSameDay } from '../../index-39306dfb.js';
import Icon, { IconTypes, IconColors } from '../../ui/Icon.js';
import { P as PlaceHolder, a as PlaceHolderTypes } from '../../index-49ff1ed1.js';
import { u as useOpenChannelContext, c as compareMessagesForGrouping } from '../../OpenChannelProvider-d0f0bf3a.js';
import MessagOpenChannelMessageeHoc from './OpenChannelMessage.js';
import { MessageProvider } from '../../Message/context.js';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import { a as useHandleOnScrollCallback } from '../../index-28ad85ee.js';
import '../../index-2dc53ac2.js';
import '../../index-67135027.js';
import 'prop-types';
import '../../LocalizationContext-b2390bf8.js';
import '../../stringSet-eefbc798.js';
import '../../index-4372fcec.js';
import '../../ui/Loader.js';
import '../../UserProfileContext-d5787c95.js';
import '../../_rollupPluginBabelHelpers-4f8ebde4.js';
import '../../compareIds-7800035f.js';
import '../../topics-f3629ea0.js';
import '@sendbird/chat';
import '@sendbird/chat/openChannel';
import '../../uuid-a79b5ba8.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-eefb44ba.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../ui/IconButton.js';
import '../../ui/UserProfile.js';
import '../../sendbirdSelectors.js';
import '../../utils-b53fe532.js';
import '../../ui/Button.js';
import '../../utils-3b418b35.js';
import '../../index-1fefb8e6.js';
import '../../MediaQueryContext-b4904a2e.js';
import '../../useLongPress-cb2d23be.js';
import '../../ui/OpenChannelAdminMessage.js';
import '../../ui/OpenchannelOGMessage.js';
import '../../ui/LinkLabel.js';
import '../../index-8aa7880e.js';
import '../../index.module-2885df8c.js';
import '../../tokenize-b97bc343.js';
import '../../ui/MentionLabel.js';
import '../../ui/OpenchannelThumbnailMessage.js';
import '../../ui/OpenchannelFileMessage.js';
import '../../ui/TextButton.js';
import '../../color-9519a526.js';
import '../../ui/DateSeparator.js';
import '../../ui/MessageInput.js';
import '../../const-922f4cc5.js';
import '../../const-97fac210.js';
import 'dompurify';
import '../../consts-8ce876a0.js';
import '../../consts-e3e03145.js';
import '../../consts-cdccf171.js';
import '../../ui/FileViewer.js';
import '../../index-896545a0.js';
import '../../ui/Modal.js';
import '../../withSendbird.js';
import '../../consts-33537265.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
