'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var LocalizationContext = require('../../LocalizationContext-8cc9717d.js');
var MessageSearch_context = require('../context.js');
var ui_MessageSearchItem = require('../../ui/MessageSearchItem.js');
var ui_PlaceHolder = require('../../index-985a1afb.js');
var ui_MessageSearchFileItem = require('../../ui/MessageSearchFileItem.js');
require('../../stringSet-ce782136.js');
require('../../index-d749c4c2.js');
require('../../useSendbirdStateContext.js');
require('../../withSendbird.js');
require('../../_rollupPluginBabelHelpers-acf412a3.js');
require('../../index-3fc4f7d7.js');
require('../../index-0e24aca4.js');
require('../../index-bf8f9551.js');
require('../../ui/Avatar.js');
require('../../ui/ImageRenderer.js');
require('../../ui/Icon.js');
require('prop-types');
require('../../uuid-1eceb0c8.js');
require('../../index-35633fb6.js');
require('../../ui/Loader.js');
require('../../index-a91b85d9.js');
require('../../utils/message/getOutgoingMessageState.js');

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

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

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
  } = MessageSearch_context.useMessageSearchContext();
  const {
    stringSet
  } = React.useContext(LocalizationContext.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["default"].createElement("div", {
      className: COMPONENT_CLASS_NAME
    }, /*#__PURE__*/React__default["default"].createElement(ui_PlaceHolder.PlaceHolder, {
      type: ui_PlaceHolder.PlaceHolderTypes.WRONG,
      retryToConnect: handleRetryToConnect
    }));
  }
  if (loading && searchString && requestString) {
    return (renderPlaceHolderLoading === null || renderPlaceHolderLoading === void 0 ? void 0 : renderPlaceHolderLoading()) || /*#__PURE__*/React__default["default"].createElement("div", {
      className: COMPONENT_CLASS_NAME
    }, /*#__PURE__*/React__default["default"].createElement(ui_PlaceHolder.PlaceHolder, {
      type: ui_PlaceHolder.PlaceHolderTypes.SEARCHING
    }));
  }
  if (!searchString) {
    return (renderPlaceHolderNoString === null || renderPlaceHolderNoString === void 0 ? void 0 : renderPlaceHolderNoString()) || /*#__PURE__*/React__default["default"].createElement("div", {
      className: COMPONENT_CLASS_NAME
    }, /*#__PURE__*/React__default["default"].createElement(ui_PlaceHolder.PlaceHolder, {
      type: ui_PlaceHolder.PlaceHolderTypes.SEARCH_IN,
      searchInString: getChannelName()
    }));
  }
  return /*#__PURE__*/React__default["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["default"].createElement(ui_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["default"].createElement(ui_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["default"].createElement(ui_PlaceHolder.PlaceHolder, {
    type: ui_PlaceHolder.PlaceHolderTypes.NO_RESULTS
  }));
};

exports.MessageSearchUI = MessageSearchUI;
exports["default"] = MessageSearchUI;
//# sourceMappingURL=MessageSearchUI.js.map
