'use strict';

var React = require('react');
var index = require('../../index-32123d95.js');
var ui_Icon = require('../../ui/Icon.js');
var ui_PlaceHolder = require('../../index-1ac7777f.js');
var OpenChannel_context = require('../../OpenChannelProvider-dd8403ee.js');
var OpenChannel_components_OpenChannelMessage = require('./OpenChannelMessage.js');
require('../../index-a11fb495.js');
require('../../index-d82f6dd1.js');
require('prop-types');
require('../../tslib.es6-7d75f2e2.js');
require('../../LocalizationContext-1f00818c.js');
require('../../stringSet-410775ff.js');
require('../../index-8d7a94e3.js');
require('../../ui/Loader.js');
require('../../UserProfileContext-3a4f0132.js');
require('../../compareIds-8d064912.js');
require('../../topics-9747398e.js');
require('../../sendbird-5edd0213.js');
require('../../__bundle-57e0c5af-b8304f7a.js');
require('../../__bundle-0dad34cd-bd827f82.js');
require('../../openChannel-c5952dd7.js');
require('../../__bundle-bd072f34-578ccb1e.js');
require('../../uuid-c813a8a1.js');
require('../../useSendbirdStateContext.js');
require('../../withSendbird.js');
require('../../_rollupPluginBabelHelpers-6917423d.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-177380fc.js');
require('../../ui/IconButton.js');
require('../../ui/UserProfile.js');
require('../../sendbirdSelectors.js');
require('../../utils-37a47883.js');
require('../../ui/Button.js');
require('../../utils-01db6d26.js');
require('../../openChannelUtils-f286fd78.js');
require('../../ui/OpenChannelAdminMessage.js');
require('../../ui/OpenchannelOGMessage.js');
require('../../ui/LinkLabel.js');
require('../../ui/Word.js');
require('../../ui/MentionLabel.js');
require('../../ui/OpenchannelThumbnailMessage.js');
require('../../ui/OpenchannelFileMessage.js');
require('../../ui/TextButton.js');
require('../../color-0a6e19c1.js');
require('../../ui/DateSeparator.js');
require('../../ui/MessageInput.js');
require('react-dom/server');
require('../../const-bf239fc5.js');
require('../../const-67a2a885.js');
require('../../ui/MentionUserLabel.js');
require('../../ui/FileViewer.js');
require('../../index-8c25d6aa.js');
require('../../ui/Modal.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) {
  var _a = OpenChannel_context.useOpenChannelContext(),
      _b = _a.isMessageGroupingEnabled,
      isMessageGroupingEnabled = _b === void 0 ? true : _b,
      allMessages = _a.allMessages,
      hasMore = _a.hasMore,
      onScroll = _a.onScroll;

  var scrollRef = ref || React.useRef(null);

  var _c = React.useState(false),
      showScrollDownButton = _c[0],
      setShowScrollDownButton = _c[1];

  var handleOnScroll = function (e) {
    var element = e.target;
    var scrollTop = element.scrollTop,
        scrollHeight = element.scrollHeight,
        clientHeight = element.clientHeight;

    if (scrollHeight > scrollTop + clientHeight + 1) {
      setShowScrollDownButton(true);
    } else {
      setShowScrollDownButton(false);
    }

    if (!hasMore) {
      return;
    }

    if (scrollTop === 0) {
      var nodes = scrollRef.current.querySelectorAll('.sendbird-msg--scroll-ref');
      var first_1 = nodes && nodes[0];
      onScroll(function () {
        try {
          first_1.scrollIntoView();
        } catch (error) {}
      });
    }
  };

  var scrollToBottom = function () {
    if (scrollRef && scrollRef.current) {
      scrollRef.current.scrollTo(0, scrollRef.current.scrollHeight);
      setShowScrollDownButton(false);
    }
  };

  var memoizedMessageList = React.useMemo(function () {
    var _a;

    if (allMessages.length > 0) {
      return allMessages.map(function (message, index$1) {
        var previousMessage = allMessages[index$1 - 1];
        var nextMessage = allMessages[index$1 - 1];
        var previousMessageCreatedAt = previousMessage && previousMessage.createdAt;
        var currentCreatedAt = message === null || message === void 0 ? void 0 : message.createdAt; // https://stackoverflow.com/a/41855608

        var hasSeparator = !(previousMessageCreatedAt && index.isSameDay(currentCreatedAt, previousMessageCreatedAt));

        var _a = isMessageGroupingEnabled ? OpenChannel_context.compareMessagesForGrouping(previousMessage, message, nextMessage) : [false, false],
            chainTop = _a[0],
            chainBottom = _a[1];

        return /*#__PURE__*/React__default["default"].createElement(OpenChannel_components_OpenChannelMessage, {
          key: (message === null || message === void 0 ? void 0 : message.messageId) || (message === null || message === void 0 ? void 0 : message.reqId),
          message: message,
          chainTop: chainTop,
          chainBottom: chainBottom,
          hasSeparator: hasSeparator,
          renderMessage: props === null || props === void 0 ? void 0 : props.renderMessage
        });
      });
    }

    return ((_a = props === null || props === void 0 ? void 0 : props.renderPlaceHolderEmptyList) === null || _a === void 0 ? void 0 : _a.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
