import React__default, { useMemo, useState, useRef } from 'react';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import { u as useLocalization } from '../../LocalizationContext-426f3d58.js';
import { g as getChannelTitle } from '../../utils-d317e3c3.js';
import { u as useThreadContext, i as isAboutSame } from '../../ThreadProvider-7f67575f.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-8b2036e8.js';
import { L as Label, a as LabelTypography, b as LabelColors } from '../../index-4105bc4d.js';
import { MessageProvider } from '../../Message/context.js';
import '../../withSendbird.js';
import '../../_rollupPluginBabelHelpers-a69fd1ce.js';
import '../../stringSet-a62108b2.js';
import '../../index-90420b99.js';
import '../../index-e7f4cbdf.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../UserProfileContext-a59f0533.js';
import 'prop-types';
import '@sendbird/chat';
import '../../topics-247fcf3a.js';
import '@sendbird/chat/groupChannel';
import '../../uuid-2e8efc75.js';
import '@sendbird/chat/message';
import '../../consts-e64103c6.js';
import '../../RemoveMessageModal-3d9ef84c.js';
import '../../ui/Modal.js';
import 'react-dom';
import '../../index-3e548496.js';
import '../../ui/Button.js';
import '../../ui/Icon.js';
import '../../ui/IconButton.js';
import '../../MediaQueryContext-b531adc9.js';
import './ParentMessageInfoItem.js';
import '../../index-f83f0aac.js';
import '../../ui/ImageRenderer.js';
import '../../ui/TextButton.js';
import '../../color-e708f53d.js';
import '../../ui/EmojiReactions.js';
import '../../ui/ReactionBadge.js';
import '../../ui/ReactionButton.js';
import '../../useLongPress-c0ba590c.js';
import '../../utils-4bf6e4e6.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-52ba73b9.js';
import '../../VoiceRecorder/context.js';
import '../../ui/PlaybackTime.js';
import '../../ui/Loader.js';
import '../../index-89c84c27.js';
import '../../index.module-8bb7760e.js';
import '../../tokenize-eec42500.js';
import '../../ui/MentionLabel.js';
import '../../ui/LinkLabel.js';
import '../../ChannelProvider-3bd92d16.js';
import '../../const-9982664a.js';
import '../../compareIds-aca19765.js';
import '../../resolvedReplyType-7bde5f56.js';
import '../../Channel/components/SuggestedMentionList.js';
import '../../const-ddd9d461.js';
import '../../ui/FileViewer.js';
import '../../ui/MessageItemMenu.js';
import '../../types-d72e53f1.js';
import '../../ui/MessageItemReactionMenu.js';
import '../../ui/MessageInput.js';
import 'dompurify';
import '../../consts-15e764c0.js';
import '../../consts-f99cd0da.js';
import '../../consts-2eeaa3de.js';
import '../../index-eefc5b10.js';
import '../../useDirtyGetMentions-f302bddb.js';
import './ThreadListItem.js';
import '../../ui/DateSeparator.js';
import '../../index-a9ca43b0.js';
import '../../index-65ed45e9.js';
import '../../index-e40d8b24.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-3adde78a.js';
import '../../VoiceRecorder/useVoiceRecorder.js';
import '../../index-8567d9a9.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
