import React__default, { useMemo, useState, useRef } from 'react';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import { u as useLocalization } from '../../LocalizationContext-5920dafc.js';
import { g as getChannelTitle } from '../../utils-ad636dfd.js';
import { u as useThreadContext, i as isAboutSame } from '../../ThreadProvider-4640c6fb.js';
import ParentMessageInfo from './ParentMessageInfo.js';
import ThreadHeader from './ThreadHeader.js';
import ThreadList from './ThreadList.js';
import ThreadMessageInput from './ThreadMessageInput.js';
import { ParentMessageInfoStateTypes, ThreadListStateTypes } from '../context/types.js';
import { P as PlaceHolder, a as PlaceHolderTypes } from '../../index-cb6348b0.js';
import { L as Label, a as LabelTypography, b as LabelColors } from '../../index-4a23e6ff.js';
import '../../withSendbird.js';
import '../../_rollupPluginBabelHelpers-636e674b.js';
import '../../stringSet-f0e25f08.js';
import '../../index-b5b56cb7.js';
import '../../index-4f51d577.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../UserProfileContext-749db4f6.js';
import 'prop-types';
import '../../tslib.es6-279bc71d.js';
import '@sendbird/chat';
import '../../topics-d2e15fe6.js';
import '@sendbird/chat/groupChannel';
import '../../uuid-636df95b.js';
import '@sendbird/chat/message';
import '../../RemoveMessageModal-0ce1f978.js';
import '../../ui/Modal.js';
import 'react-dom';
import '../../index-d2dd3ae2.js';
import '../../ui/Button.js';
import '../../ui/Icon.js';
import '../../ui/IconButton.js';
import '../../MediaQueryContext-9c1bb79f.js';
import './ParentMessageInfoItem.js';
import '../../index-1a59aaa9.js';
import '../../ui/Word.js';
import '../../ui/LinkLabel.js';
import '../../ui/MentionLabel.js';
import '../../ui/ContextMenu.js';
import '../../ui/SortByRow.js';
import '../../ui/UserProfile.js';
import '../../sendbirdSelectors.js';
import '../../utils-5a4995e3.js';
import '../../ui/Avatar.js';
import '../../ui/ImageRenderer.js';
import '../../ui/TextButton.js';
import '../../color-253fcdc4.js';
import '../../ui/EmojiReactions.js';
import '../../ui/Tooltip.js';
import '../../ui/TooltipWrapper.js';
import '../../ui/ReactionBadge.js';
import '../../ui/ReactionButton.js';
import '../../Channel/components/SuggestedMentionList.js';
import '../../ChannelProvider-8be62d5d.js';
import '../../compareIds-5494c074.js';
import '../../const-8bb8cc10.js';
import '../../const-2aae43f2.js';
import '../../ui/FileViewer.js';
import '../../ui/MessageItemMenu.js';
import '../../ui/MessageItemReactionMenu.js';
import '../../ui/MessageInput.js';
import 'react-dom/server';
import '../../ui/MentionUserLabel.js';
import './ThreadListItem.js';
import '../../ui/DateSeparator.js';
import '../../ui/MessageStatus.js';
import '../../ui/Loader.js';
import '../../ui/TextMessageItemBody.js';
import '../../ui/OGMessageItemBody.js';
import '../../ui/FileMessageItemBody.js';
import '../../ui/ThumbnailMessageItemBody.js';
import '../../ui/UnknownMessageItemBody.js';
import 'date-fns';

var useMemorizedHeader = function (_a) {
  var renderHeader = _a.renderHeader;
  return 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 useMemo(function () {
    if (parentMessageInfoStatus === ParentMessageInfoStateTypes.NIL || parentMessageInfoStatus === ParentMessageInfoStateTypes.LOADING || parentMessageInfoStatus === ParentMessageInfoStateTypes.INVALID) {
      if (typeof renderParentMessageInfoPlaceholder === 'function') {
        return renderParentMessageInfoPlaceholder(parentMessageInfoStatus);
      }

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

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

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

        default:
          {
            return null;
          }
      }
    } else if (parentMessageInfoStatus === 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 useMemo(function () {
    if (threadListStatus === ThreadListStateTypes.NIL || threadListStatus === ThreadListStateTypes.LOADING || threadListStatus === ThreadListStateTypes.INVALID) {
      if (typeof renderThreadListPlaceHolder === 'function') {
        return renderThreadListPlaceHolder(threadListStatus);
      }

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

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

        case ThreadListStateTypes.NIL:
          {
            return /*#__PURE__*/React__default.createElement(React__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 = useLocalization().stringSet;

  var _e = 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 = useState(0),
      scrollBottom = _f[0],
      setScrollBottom = _f[1];

  var scrollRef = 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 (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 (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.createElement("div", {
    className: "sendbird-thread-ui"
  }, MemorizedHeader || /*#__PURE__*/React__default.createElement(ThreadHeader, {
    className: "sendbird-thread-ui__header",
    channelName: getChannelTitle(currentChannel, currentUserId, stringSet),
    onActionIconClick: onHeaderActionClick,
    onChannelNameClick: function () {
      return onMoveToParentMessage({
        message: parentMessage,
        channel: currentChannel
      });
    }
  }), /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-thread-ui--scroll",
    ref: scrollRef,
    onScroll: onScroll
  }, MemorizedParentMessageInfo || /*#__PURE__*/React__default.createElement(ParentMessageInfo, {
    className: "sendbird-thread-ui__parent-message-info"
  }), replyCount > 0 && /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-thread-ui__reply-counts"
  }, /*#__PURE__*/React__default.createElement(Label, {
    type: LabelTypography.BODY_1,
    color: LabelColors.ONBACKGROUND_3
  }, "".concat(replyCount, " ").concat(replyCount > 1 ? stringSet.THREAD__THREAD_REPLIES : stringSet.THREAD__THREAD_REPLY))), MemorizedThreadList || /*#__PURE__*/React__default.createElement(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.createElement(ThreadMessageInput, {
    className: "sendbird-thread-ui__message-input"
  })));
};

export { ThreadUI as default };
//# sourceMappingURL=ThreadUI.js.map
