'use strict';

var React = require('react');
var useSendbirdStateContext = require('../../useSendbirdStateContext.js');
var LocalizationContext = require('../../LocalizationContext-9a1e0d36.js');
var utils = require('../../utils-7a127b25.js');
var Thread_context = require('../../ThreadProvider-8ba81f66.js');
var Thread_components_ParentMessageInfo = require('./ParentMessageInfo.js');
var Thread_components_ThreadHeader = require('./ThreadHeader.js');
var Thread_components_ThreadList = require('./ThreadList.js');
var Thread_components_ThreadMessageInput = require('./ThreadMessageInput.js');
var Thread_context_types = require('../context/types.js');
var ui_PlaceHolder = require('../../index-aa5520c6.js');
var ui_Label = require('../../index-d714b895.js');
require('../../withSendbird.js');
require('../../_rollupPluginBabelHelpers-ada5a2bb.js');
require('../../stringSet-2696d00d.js');
require('../../index-6ca5c151.js');
require('../../index-5bc31321.js');
require('../../utils/message/getOutgoingMessageState.js');
require('../../UserProfileContext-2648d24c.js');
require('prop-types');
require('../../tslib.es6-259cb5ca.js');
require('@sendbird/chat');
require('../../topics-528fde56.js');
require('@sendbird/chat/groupChannel');
require('../../uuid-4e015383.js');
require('@sendbird/chat/message');
require('../../RemoveMessageModal-b64c35a1.js');
require('../../ui/Modal.js');
require('react-dom');
require('../../index-3d069920.js');
require('../../ui/Button.js');
require('../../ui/Icon.js');
require('../../ui/IconButton.js');
require('../../MediaQueryContext-7f516d81.js');
require('./ParentMessageInfoItem.js');
require('../../index-b5c769e1.js');
require('../../ui/Word.js');
require('../../ui/LinkLabel.js');
require('../../ui/MentionLabel.js');
require('../../ui/ContextMenu.js');
require('../../ui/SortByRow.js');
require('../../ui/UserProfile.js');
require('../../sendbirdSelectors.js');
require('../../utils-afb14420.js');
require('../../ui/Avatar.js');
require('../../ui/ImageRenderer.js');
require('../../ui/TextButton.js');
require('../../color-4b850c2c.js');
require('../../ui/EmojiReactions.js');
require('../../ui/Tooltip.js');
require('../../ui/TooltipWrapper.js');
require('../../ui/ReactionBadge.js');
require('../../ui/ReactionButton.js');
require('../../Channel/components/SuggestedMentionList.js');
require('../../ChannelProvider-a765f8a9.js');
require('../../compareIds-03d1c5fd.js');
require('../../const-cd2f7184.js');
require('../../const-5080a6f8.js');
require('../../ui/FileViewer.js');
require('../../ui/MessageItemMenu.js');
require('../../ui/MessageItemReactionMenu.js');
require('../../ui/MessageInput.js');
require('react-dom/server');
require('../../ui/MentionUserLabel.js');
require('./ThreadListItem.js');
require('../../ui/DateSeparator.js');
require('../../ui/MessageStatus.js');
require('../../ui/Loader.js');
require('../../ui/TextMessageItemBody.js');
require('../../ui/OGMessageItemBody.js');
require('../../ui/FileMessageItemBody.js');
require('../../ui/ThumbnailMessageItemBody.js');
require('../../ui/UnknownMessageItemBody.js');
require('date-fns');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

var useMemorizedHeader = function (_a) {
  var renderHeader = _a.renderHeader;
  return React.useMemo(function () {
    if (typeof renderHeader === 'function') {
      return renderHeader();
    }

    return null;
  }, [renderHeader]);
};

var useMemorizedParentMessageInfo = function (_a) {
  var parentMessage = _a.parentMessage,
      parentMessageInfoStatus = _a.parentMessageInfoStatus,
      renderParentMessageInfo = _a.renderParentMessageInfo,
      renderParentMessageInfoPlaceholder = _a.renderParentMessageInfoPlaceholder;
  return React.useMemo(function () {
    if (parentMessageInfoStatus === Thread_context_types.ParentMessageInfoStateTypes.NIL || parentMessageInfoStatus === Thread_context_types.ParentMessageInfoStateTypes.LOADING || parentMessageInfoStatus === Thread_context_types.ParentMessageInfoStateTypes.INVALID) {
      if (typeof renderParentMessageInfoPlaceholder === 'function') {
        return renderParentMessageInfoPlaceholder(parentMessageInfoStatus);
      }

      switch (parentMessageInfoStatus) {
        case Thread_context_types.ParentMessageInfoStateTypes.NIL:
          {
            return /*#__PURE__*/React__default["default"].createElement(ui_PlaceHolder.PlaceHolder, {
              className: "sendbird-thread-ui__parent-message-info placeholder-nil",
              type: ui_PlaceHolder.PlaceHolderTypes.NO_RESULTS,
              iconSize: "64px"
            });
          }

        case Thread_context_types.ParentMessageInfoStateTypes.LOADING:
          {
            return /*#__PURE__*/React__default["default"].createElement(ui_PlaceHolder.PlaceHolder, {
              className: "sendbird-thread-ui__parent-message-info placeholder-loading",
              type: ui_PlaceHolder.PlaceHolderTypes.LOADING,
              iconSize: "64px"
            });
          }

        case Thread_context_types.ParentMessageInfoStateTypes.INVALID:
          {
            return /*#__PURE__*/React__default["default"].createElement(ui_PlaceHolder.PlaceHolder, {
              className: "sendbird-thread-ui__parent-message-info placeholder-invalid",
              type: ui_PlaceHolder.PlaceHolderTypes.WRONG,
              iconSize: "64px"
            });
          }

        default:
          {
            return null;
          }
      }
    } else if (parentMessageInfoStatus === Thread_context_types.ParentMessageInfoStateTypes.INITIALIZED) {
      if (typeof renderParentMessageInfo === 'function') {
        return renderParentMessageInfo();
      }
    }

    return null;
  }, [parentMessage, parentMessageInfoStatus, renderParentMessageInfo, renderParentMessageInfoPlaceholder]);
};

var useMemorizedThreadList = function (_a) {
  var threadListStatus = _a.threadListStatus,
      renderThreadListPlaceHolder = _a.renderThreadListPlaceHolder;
  return React.useMemo(function () {
    if (threadListStatus === Thread_context_types.ThreadListStateTypes.NIL || threadListStatus === Thread_context_types.ThreadListStateTypes.LOADING || threadListStatus === Thread_context_types.ThreadListStateTypes.INVALID) {
      if (typeof renderThreadListPlaceHolder === 'function') {
        return renderThreadListPlaceHolder(threadListStatus);
      }

      switch (threadListStatus) {
        case Thread_context_types.ThreadListStateTypes.LOADING:
          {
            return /*#__PURE__*/React__default["default"].createElement(ui_PlaceHolder.PlaceHolder, {
              className: "sendbird-thread-ui__thread-list placeholder-loading",
              type: ui_PlaceHolder.PlaceHolderTypes.LOADING,
              iconSize: "64px"
            });
          }

        case Thread_context_types.ThreadListStateTypes.INVALID:
          {
            return /*#__PURE__*/React__default["default"].createElement(ui_PlaceHolder.PlaceHolder, {
              className: "sendbird-thread-ui__thread-list placeholder-invalid",
              type: ui_PlaceHolder.PlaceHolderTypes.WRONG,
              iconSize: "64px"
            });
          }

        case Thread_context_types.ThreadListStateTypes.NIL:
          {
            return /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null);
          }

        default:
          {
            return null;
          }
      }
    }

    return null;
  }, [threadListStatus, renderThreadListPlaceHolder]);
};

var ThreadUI = function (_a) {
  var _b, _c, _d;

  var renderHeader = _a.renderHeader,
      renderParentMessageInfo = _a.renderParentMessageInfo,
      renderMessage = _a.renderMessage,
      renderMessageInput = _a.renderMessageInput,
      renderCustomSeparator = _a.renderCustomSeparator,
      renderParentMessageInfoPlaceholder = _a.renderParentMessageInfoPlaceholder,
      renderThreadListPlaceHolder = _a.renderThreadListPlaceHolder;
  var stores = useSendbirdStateContext().stores;
  var currentUserId = (_d = (_c = (_b = stores === null || stores === void 0 ? void 0 : stores.sdkStore) === null || _b === void 0 ? void 0 : _b.sdk) === null || _c === void 0 ? void 0 : _c.currentUser) === null || _d === void 0 ? void 0 : _d.userId;
  var stringSet = LocalizationContext.useLocalization().stringSet;

  var _e = Thread_context.useThreadContext(),
      currentChannel = _e.currentChannel,
      allThreadMessages = _e.allThreadMessages,
      parentMessage = _e.parentMessage,
      parentMessageInfoStatus = _e.parentMessageInfoStatus,
      threadListStatus = _e.threadListStatus,
      hasMorePrev = _e.hasMorePrev,
      hasMoreNext = _e.hasMoreNext,
      fetchPrevThreads = _e.fetchPrevThreads,
      fetchNextThreads = _e.fetchNextThreads,
      onHeaderActionClick = _e.onHeaderActionClick,
      onMoveToParentMessage = _e.onMoveToParentMessage;

  var replyCount = allThreadMessages.length; // Memoized custom components

  var MemorizedHeader = useMemorizedHeader({
    renderHeader: renderHeader
  });
  var MemorizedParentMessageInfo = useMemorizedParentMessageInfo({
    parentMessage: parentMessage,
    parentMessageInfoStatus: parentMessageInfoStatus,
    renderParentMessageInfo: renderParentMessageInfo,
    renderParentMessageInfoPlaceholder: renderParentMessageInfoPlaceholder
  });
  var MemorizedThreadList = useMemorizedThreadList({
    threadListStatus: threadListStatus,
    renderThreadListPlaceHolder: renderThreadListPlaceHolder
  }); // scroll

  var _f = React.useState(0),
      scrollBottom = _f[0],
      setScrollBottom = _f[1];

  var scrollRef = React.useRef(null);

  var onScroll = function (e) {
    var element = e.target;
    var scrollTop = element.scrollTop,
        clientHeight = element.clientHeight,
        scrollHeight = element.scrollHeight;
    var threadItemNodes = scrollRef.current.querySelectorAll('.sendbird-thread-list-item');
    var firstNode = threadItemNodes === null || threadItemNodes === void 0 ? void 0 : threadItemNodes[0];

    if (Thread_context.isAboutSame(scrollTop, 0, 10) && hasMorePrev) {
      fetchPrevThreads(function (messages) {
        var _a;

        if (messages) {
          try {
            (_a = firstNode === null || firstNode === void 0 ? void 0 : firstNode.scrollIntoView) === null || _a === void 0 ? void 0 : _a.call(firstNode, {
              block: 'start',
              inline: 'nearest'
            });
          } catch (error) {//
          }
        }
      });
    }

    if (Thread_context.isAboutSame(clientHeight + scrollTop, scrollHeight, 10) && hasMoreNext) {
      var scrollTop_1 = scrollTop;
      fetchNextThreads(function (messages) {
        if (messages) {
          try {
            element.scrollTop = scrollTop_1;
            scrollRef.current.scrollTop = scrollTop_1;
          } catch (error) {//
          }
        }
      });
    } // save the lastest scroll bottom value


    if (scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current) {
      var current = scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current;
      setScrollBottom(current.scrollHeight - current.scrollTop - current.offsetHeight);
    }
  };

  return /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-thread-ui"
  }, MemorizedHeader || /*#__PURE__*/React__default["default"].createElement(Thread_components_ThreadHeader, {
    className: "sendbird-thread-ui__header",
    channelName: utils.getChannelTitle(currentChannel, currentUserId, stringSet),
    onActionIconClick: onHeaderActionClick,
    onChannelNameClick: function () {
      return onMoveToParentMessage({
        message: parentMessage,
        channel: currentChannel
      });
    }
  }), /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-thread-ui--scroll",
    ref: scrollRef,
    onScroll: onScroll
  }, MemorizedParentMessageInfo || /*#__PURE__*/React__default["default"].createElement(Thread_components_ParentMessageInfo, {
    className: "sendbird-thread-ui__parent-message-info"
  }), replyCount > 0 && /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-thread-ui__reply-counts"
  }, /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    type: ui_Label.LabelTypography.BODY_1,
    color: ui_Label.LabelColors.ONBACKGROUND_3
  }, "".concat(replyCount, " ").concat(replyCount > 1 ? stringSet.THREAD__THREAD_REPLIES : stringSet.THREAD__THREAD_REPLY))), MemorizedThreadList || /*#__PURE__*/React__default["default"].createElement(Thread_components_ThreadList, {
    className: "sendbird-thread-ui__thread-list",
    allThreadMessages: allThreadMessages,
    renderMessage: renderMessage,
    renderCustomSeparator: renderCustomSeparator,
    scrollRef: scrollRef,
    scrollBottom: scrollBottom
  }), (renderMessageInput === null || renderMessageInput === void 0 ? void 0 : renderMessageInput()) || /*#__PURE__*/React__default["default"].createElement(Thread_components_ThreadMessageInput, {
    className: "sendbird-thread-ui__message-input"
  })));
};

module.exports = ThreadUI;
//# sourceMappingURL=ThreadUI.js.map
