import React__default, { useContext } from 'react';
import { a as LocalizationContext } from '../../LocalizationContext-b2390bf8.js';
import { useMessageSearchContext } from '../context.js';
import MessageSearchItem from '../../ui/MessageSearchItem.js';
import { P as PlaceHolder, a as PlaceHolderTypes } from '../../index-49ff1ed1.js';
import MessageSearchFileItem from '../../ui/MessageSearchFileItem.js';
import '../../stringSet-eefbc798.js';
import '../../index-67135027.js';
import '../../useSendbirdStateContext.js';
import '../../withSendbird.js';
import '../../_rollupPluginBabelHelpers-4f8ebde4.js';
import '../../index-2dc53ac2.js';
import '../../index-0d1114ee.js';
import '../../index-39306dfb.js';
import '../../ui/Avatar.js';
import '../../ui/ImageRenderer.js';
import '../../ui/Icon.js';
import 'prop-types';
import '../../uuid-a79b5ba8.js';
import '../../index-4372fcec.js';
import '../../ui/Loader.js';
import '../../index-eefb44ba.js';
import '../../utils/message/getOutgoingMessageState.js';

const COMPONENT_CLASS_NAME = 'sendbird-message-search';
const MessageSearchUI = _ref => {
  let {
    renderPlaceHolderError,
    renderPlaceHolderLoading,
    renderPlaceHolderNoString,
    renderPlaceHolderEmptyList,
    renderSearchItem
  } = _ref;
  const {
    isInvalid,
    searchString,
    requestString,
    currentChannel,
    retryCount,
    setRetryCount,
    loading,
    scrollRef,
    hasMoreResult,
    onScroll,
    allMessages,
    onResultClick,
    selectedMessageId,
    setSelectedMessageId
  } = useMessageSearchContext();
  const {
    stringSet
  } = useContext(LocalizationContext);
  const handleRetryToConnect = () => {
    setRetryCount(retryCount + 1);
  };
  const handleOnScroll = e => {
    const scrollElement = e.target;
    const {
      scrollTop,
      scrollHeight,
      clientHeight
    } = scrollElement;
    if (!hasMoreResult) {
      return;
    }
    if (scrollTop + clientHeight >= scrollHeight - 1) {
      onScroll(() => {
        // after load more searched messages
      });
    }
  };
  const getChannelName = () => {
    if (currentChannel && currentChannel !== null && currentChannel !== void 0 && currentChannel.name && (currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.name) !== 'Group Channel') {
      return currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.name;
    }
    if (currentChannel && ((currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.name) === 'Group Channel' || !(currentChannel !== null && currentChannel !== void 0 && currentChannel.name))) {
      return currentChannel.members.map(member => member.nickname || stringSet.NO_NAME).join(', ');
    }
    return stringSet.NO_TITLE;
  };
  if (isInvalid && searchString && requestString) {
    return (renderPlaceHolderError === null || renderPlaceHolderError === void 0 ? void 0 : renderPlaceHolderError()) || /*#__PURE__*/React__default.createElement("div", {
      className: COMPONENT_CLASS_NAME
    }, /*#__PURE__*/React__default.createElement(PlaceHolder, {
      type: PlaceHolderTypes.WRONG,
      retryToConnect: handleRetryToConnect
    }));
  }
  if (loading && searchString && requestString) {
    return (renderPlaceHolderLoading === null || renderPlaceHolderLoading === void 0 ? void 0 : renderPlaceHolderLoading()) || /*#__PURE__*/React__default.createElement("div", {
      className: COMPONENT_CLASS_NAME
    }, /*#__PURE__*/React__default.createElement(PlaceHolder, {
      type: PlaceHolderTypes.SEARCHING
    }));
  }
  if (!searchString) {
    return (renderPlaceHolderNoString === null || renderPlaceHolderNoString === void 0 ? void 0 : renderPlaceHolderNoString()) || /*#__PURE__*/React__default.createElement("div", {
      className: COMPONENT_CLASS_NAME
    }, /*#__PURE__*/React__default.createElement(PlaceHolder, {
      type: PlaceHolderTypes.SEARCH_IN,
      searchInString: getChannelName()
    }));
  }
  return /*#__PURE__*/React__default.createElement("div", {
    className: COMPONENT_CLASS_NAME,
    onScroll: handleOnScroll,
    ref: scrollRef
  }, allMessages.length > 0 ? allMessages.map(message => {
    if (renderSearchItem) {
      return renderSearchItem({
        message,
        onResultClick
      });
    }
    if (message.messageType === 'file') {
      return /*#__PURE__*/React__default.createElement(MessageSearchFileItem, {
        className: `${COMPONENT_CLASS_NAME}__message-search-item`,
        message: message,
        key: message.messageId,
        selected: selectedMessageId === message.messageId,
        onClick: () => {
          onResultClick(message);
          setSelectedMessageId(message.messageId);
        }
      });
    }
    return /*#__PURE__*/React__default.createElement(MessageSearchItem, {
      className: `${COMPONENT_CLASS_NAME}__message-search-item`,
      message: message,
      key: message.messageId,
      selected: selectedMessageId === message.messageId,
      onClick: () => {
        onResultClick(message);
        setSelectedMessageId(message.messageId);
      }
    });
  }) : (renderPlaceHolderEmptyList === null || renderPlaceHolderEmptyList === void 0 ? void 0 : renderPlaceHolderEmptyList()) || /*#__PURE__*/React__default.createElement(PlaceHolder, {
    type: PlaceHolderTypes.NO_RESULTS
  }));
};

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