import React__default, { useMemo, useState, useRef } from 'react';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import { u as useLocalization } from '../../LocalizationContext-1701e5c8.js';
import { g as getChannelTitle } from '../../utils-f1b4b34b.js';
import { u as useThreadContext, i as isAboutSame } from '../../ThreadProvider-dea308a0.js';
import ParentMessageInfo from './ParentMessageInfo.js';
import ThreadHeader from './ThreadHeader.js';
import ThreadList from './ThreadList.js';
import ThreadMessageInput from './ThreadMessageInput.js';
import { ParentMessageStateTypes, ThreadListStateTypes } from '../context/types.js';
import { P as PlaceHolder, a as PlaceHolderTypes } from '../../index-11fe17e1.js';
import { L as Label, a as LabelTypography, b as LabelColors } from '../../index-f50d7ab0.js';
import { MessageProvider } from '../../Message/context.js';
import '../../withSendbird.js';
import '../../_rollupPluginBabelHelpers-cc035350.js';
import '../../stringSet-e9bb0333.js';
import '../../index-ff8f2f61.js';
import '../../index-5a62f6b7.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../UserProfileContext-74e9a128.js';
import 'prop-types';
import '@sendbird/chat';
import '../../topics-0b30f52b.js';
import '@sendbird/chat/groupChannel';
import '../../uuid-8b344dd2.js';
import '@sendbird/chat/message';
import '../../consts-44eaef6b.js';
import '../../RemoveMessageModal-bf0ab3b2.js';
import '../../ui/Modal.js';
import 'react-dom';
import '../../index-44c86e58.js';
import '../../ui/Button.js';
import '../../ui/Icon.js';
import '../../ui/IconButton.js';
import '../../MediaQueryContext-e5b401d0.js';
import './ParentMessageInfoItem.js';
import '../../index-99ab01f6.js';
import '../../ui/ImageRenderer.js';
import '../../ui/TextButton.js';
import '../../color-ea3bf5b8.js';
import '../../ui/EmojiReactions.js';
import '../../ui/ReactionBadge.js';
import '../../ui/ReactionButton.js';
import '../../useLongPress-6d0b6619.js';
import '../../utils-05aa9060.js';
import '../../ui/ContextMenu.js';
import '../../ui/SortByRow.js';
import '../../ui/BottomSheet.js';
import '../../ui/UserListItem.js';
import '../../ui/Avatar.js';
import '../../ui/MutedAvatarOverlay.js';
import '../../ui/Checkbox.js';
import '../../ui/UserProfile.js';
import '../../sendbirdSelectors.js';
import '../../ui/Tooltip.js';
import '../../ui/TooltipWrapper.js';
import '../../ui/VoiceMessageItemBody.js';
import '../../ui/ProgressBar.js';
import '../../VoicePlayer/useVoicePlayer.js';
import '../../index-02ceed0a.js';
import '../../VoiceRecorder/context.js';
import '../../ui/PlaybackTime.js';
import '../../ui/Loader.js';
import '../../index-a31ee68d.js';
import '../../index.module-d24badee.js';
import '../../tokenize-d12690c3.js';
import '../../ui/MentionLabel.js';
import '../../ui/LinkLabel.js';
import '../../ChannelProvider-1dc36682.js';
import '../../const-7c266a95.js';
import '../../compareIds-367bb030.js';
import '../../resolvedReplyType-6bb8f548.js';
import '../../Channel/components/SuggestedMentionList.js';
import '../../const-2e67461b.js';
import '../../ui/FileViewer.js';
import '../../ui/MessageItemMenu.js';
import '../../types-28e84810.js';
import '../../ui/MessageItemReactionMenu.js';
import '../../ui/MessageInput.js';
import 'dompurify';
import '../../consts-a0834b70.js';
import '../../consts-3a33a0a8.js';
import '../../consts-b3274b3d.js';
import '../../index-35696958.js';
import '../../useDirtyGetMentions-2df51ec3.js';
import './ThreadListItem.js';
import '../../ui/DateSeparator.js';
import '../../index-0aa8cbad.js';
import '../../index-7e5538e6.js';
import '../../index-3f8e1a51.js';
import '../../ui/TextMessageItemBody.js';
import '../../ui/OGMessageItemBody.js';
import '../../ui/FileMessageItemBody.js';
import '../../ui/ThumbnailMessageItemBody.js';
import '../../ui/UnknownMessageItemBody.js';
import 'date-fns';
import '../../VoiceMessageInputWrapper-4625a847.js';
import '../../VoiceRecorder/useVoiceRecorder.js';
import '../../index-cac27d56.js';

const useMemorizedHeader = _ref => {
  let {
    renderHeader
  } = _ref;
  return useMemo(() => {
    if (typeof renderHeader === 'function') {
      return renderHeader();
    }
    return null;
  }, [renderHeader]);
};

const useMemorizedParentMessageInfo = _ref => {
  let {
    parentMessage,
    parentMessageState,
    renderParentMessageInfo,
    renderParentMessageInfoPlaceholder
  } = _ref;
  return useMemo(() => {
    if (parentMessageState === ParentMessageStateTypes.NIL || parentMessageState === ParentMessageStateTypes.LOADING || parentMessageState === ParentMessageStateTypes.INVALID) {
      if (typeof renderParentMessageInfoPlaceholder === 'function') {
        return renderParentMessageInfoPlaceholder(parentMessageState);
      }
      switch (parentMessageState) {
        case ParentMessageStateTypes.NIL:
          {
            return /*#__PURE__*/React__default.createElement(PlaceHolder, {
              className: "sendbird-thread-ui__parent-message-info placeholder-nil",
              type: PlaceHolderTypes.NO_RESULTS,
              iconSize: "64px"
            });
          }
        case ParentMessageStateTypes.LOADING:
          {
            return /*#__PURE__*/React__default.createElement(PlaceHolder, {
              className: "sendbird-thread-ui__parent-message-info placeholder-loading",
              type: PlaceHolderTypes.LOADING,
              iconSize: "64px"
            });
          }
        case ParentMessageStateTypes.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 (parentMessageState === ParentMessageStateTypes.INITIALIZED) {
      if (typeof renderParentMessageInfo === 'function') {
        return renderParentMessageInfo();
      }
    }
    return null;
  }, [parentMessage, parentMessageState, renderParentMessageInfo, renderParentMessageInfoPlaceholder]);
};

const useMemorizedThreadList = _ref => {
  let {
    threadListState,
    renderThreadListPlaceHolder
  } = _ref;
  return useMemo(() => {
    if (threadListState === ThreadListStateTypes.NIL || threadListState === ThreadListStateTypes.LOADING || threadListState === ThreadListStateTypes.INVALID) {
      if (typeof renderThreadListPlaceHolder === 'function') {
        return renderThreadListPlaceHolder(threadListState);
      }
      switch (threadListState) {
        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;
  }, [threadListState, renderThreadListPlaceHolder]);
};

const ThreadUI = _ref => {
  var _stores$sdkStore, _stores$sdkStore$sdk, _stores$sdkStore$sdk$, _parentMessage$sender;
  let {
    renderHeader,
    renderParentMessageInfo,
    renderMessage,
    renderMessageInput,
    renderCustomSeparator,
    renderParentMessageInfoPlaceholder,
    renderThreadListPlaceHolder,
    renderFileUploadIcon,
    renderVoiceMessageIcon,
    renderSendMessageIcon
  } = _ref;
  const {
    stores
  } = useSendbirdStateContext();
  const currentUserId = stores === null || stores === void 0 ? void 0 : (_stores$sdkStore = stores.sdkStore) === null || _stores$sdkStore === void 0 ? void 0 : (_stores$sdkStore$sdk = _stores$sdkStore.sdk) === null || _stores$sdkStore$sdk === void 0 ? void 0 : (_stores$sdkStore$sdk$ = _stores$sdkStore$sdk.currentUser) === null || _stores$sdkStore$sdk$ === void 0 ? void 0 : _stores$sdkStore$sdk$.userId;
  const {
    stringSet
  } = useLocalization();
  const {
    currentChannel,
    allThreadMessages,
    parentMessage,
    parentMessageState,
    threadListState,
    hasMorePrev,
    hasMoreNext,
    fetchPrevThreads,
    fetchNextThreads,
    onHeaderActionClick,
    onMoveToParentMessage
  } = useThreadContext();
  const replyCount = allThreadMessages.length;
  const isByMe = currentUserId === (parentMessage === null || parentMessage === void 0 ? void 0 : (_parentMessage$sender = parentMessage.sender) === null || _parentMessage$sender === void 0 ? void 0 : _parentMessage$sender.userId);

  // Memoized custom components
  const MemorizedHeader = useMemorizedHeader({
    renderHeader
  });
  const MemorizedParentMessageInfo = useMemorizedParentMessageInfo({
    parentMessage,
    parentMessageState,
    renderParentMessageInfo,
    renderParentMessageInfoPlaceholder // nil, loading, invalid
  });

  const MemorizedThreadList = useMemorizedThreadList({
    threadListState,
    renderThreadListPlaceHolder
  });

  // scroll
  const [scrollBottom, setScrollBottom] = useState(0);
  const scrollRef = useRef(null);
  const onScroll = e => {
    var _scrollRef$current;
    const element = e.target;
    const {
      scrollTop,
      clientHeight,
      scrollHeight
    } = element;
    const threadItemNodes = (_scrollRef$current = scrollRef.current) === null || _scrollRef$current === void 0 ? void 0 : _scrollRef$current.querySelectorAll('.sendbird-thread-list-item');
    const firstNode = threadItemNodes === null || threadItemNodes === void 0 ? void 0 : threadItemNodes[0];
    if (isAboutSame(scrollTop, 0, 10) && hasMorePrev) {
      fetchPrevThreads(messages => {
        if (messages) {
          try {
            var _firstNode$scrollInto;
            firstNode === null || firstNode === void 0 ? void 0 : (_firstNode$scrollInto = firstNode.scrollIntoView) === null || _firstNode$scrollInto === void 0 ? void 0 : _firstNode$scrollInto.call(firstNode, {
              block: 'start',
              inline: 'nearest'
            });
          } catch (error) {
            //
          }
        }
      });
    }
    if (isAboutSame(clientHeight + scrollTop, scrollHeight, 10) && hasMoreNext) {
      const scrollTop_ = scrollTop;
      fetchNextThreads(messages => {
        if (messages) {
          try {
            element.scrollTop = scrollTop_;
            scrollRef.current.scrollTop = scrollTop_;
          } catch (error) {
            //
          }
        }
      });
    }

    // save the lastest scroll bottom value
    if (scrollRef !== null && scrollRef !== void 0 && scrollRef.current) {
      const 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: () => {
      onMoveToParentMessage === null || onMoveToParentMessage === void 0 ? void 0 : onMoveToParentMessage({
        message: parentMessage,
        channel: currentChannel
      });
    }
  }), /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-thread-ui--scroll",
    ref: scrollRef,
    onScroll: onScroll
  }, /*#__PURE__*/React__default.createElement(MessageProvider, {
    message: parentMessage,
    isByMe: isByMe
  }, 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
  }, `${replyCount} ${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",
    renderFileUploadIcon: renderFileUploadIcon,
    renderVoiceMessageIcon: renderVoiceMessageIcon,
    renderSendMessageIcon: renderSendMessageIcon
  }));
};

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