import React__default, { useRef, useState, useMemo } from 'react';
import { i as isSameDay } from '../../index-e40d8b24.js';
import Icon, { IconTypes, IconColors } from '../../ui/Icon.js';
import { P as PlaceHolder, a as PlaceHolderTypes } from '../../index-8b2036e8.js';
import { u as useOpenChannelContext, c as compareMessagesForGrouping } from '../../OpenChannelProvider-1efcabea.js';
import MessagOpenChannelMessageeHoc from './OpenChannelMessage.js';
import { MessageProvider } from '../../Message/context.js';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import { a as useHandleOnScrollCallback } from '../../index-80b9e974.js';
import '../../index-e7f4cbdf.js';
import '../../index-90420b99.js';
import 'prop-types';
import '../../LocalizationContext-426f3d58.js';
import '../../stringSet-a62108b2.js';
import '../../index-4105bc4d.js';
import '../../ui/Loader.js';
import '../../UserProfileContext-a59f0533.js';
import '../../_rollupPluginBabelHelpers-a69fd1ce.js';
import '../../compareIds-aca19765.js';
import '../../topics-247fcf3a.js';
import '@sendbird/chat';
import '@sendbird/chat/openChannel';
import '../../uuid-2e8efc75.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-f83f0aac.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../ui/IconButton.js';
import '../../ui/UserProfile.js';
import '../../sendbirdSelectors.js';
import '../../utils-4bf6e4e6.js';
import '../../ui/Button.js';
import '../../utils-8e1b51bf.js';
import '../../index-2528bdee.js';
import '../../MediaQueryContext-b531adc9.js';
import '../../useLongPress-c0ba590c.js';
import '../../ui/OpenChannelAdminMessage.js';
import '../../ui/OpenchannelOGMessage.js';
import '../../ui/LinkLabel.js';
import '../../index-89c84c27.js';
import '../../index.module-8bb7760e.js';
import '../../tokenize-eec42500.js';
import '../../ui/MentionLabel.js';
import '../../ui/OpenchannelThumbnailMessage.js';
import '../../ui/OpenchannelFileMessage.js';
import '../../ui/TextButton.js';
import '../../color-e708f53d.js';
import '../../ui/DateSeparator.js';
import '../../ui/MessageInput.js';
import '../../const-ddd9d461.js';
import '../../const-9982664a.js';
import 'dompurify';
import '../../consts-15e764c0.js';
import '../../consts-f99cd0da.js';
import '../../consts-2eeaa3de.js';
import '../../ui/FileViewer.js';
import '../../index-3e548496.js';
import '../../ui/Modal.js';
import '../../withSendbird.js';
import '../../consts-e64103c6.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
