"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useAmityGlobalSearchViewModel = void 0;
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _react = require("react");
var _enumTabName = require("../enums/enumTabName");
const useAmityGlobalSearchViewModel = (searchValue, searchType) => {
  const [onNextCommunityPage, setOnNextCommunityPage] = (0, _react.useState)(null);
  const [onNextMyCommunityPage, setMyOnNextCommunityPage] = (0, _react.useState)(null);
  const [onNextUserPage, setOnNextUserPage] = (0, _react.useState)(null);
  const [searchResult, setSearchResult] = (0, _react.useState)(null);
  const [isLoading, setLoading] = (0, _react.useState)(false);
  (0, _react.useEffect)(() => {
    if (!searchValue || (searchValue === null || searchValue === void 0 ? void 0 : searchValue.length) < 1) return setSearchResult(null);
    if (searchType === _enumTabName.TabName.MyCommunities) {
      const unsubscribeCommunity = _tsSdkReactNative.CommunityRepository.searchCommunities({
        displayName: searchValue,
        membership: 'member',
        limit: 20,
        sortBy: 'displayName'
      }, ({
        error,
        loading,
        data,
        hasNextPage,
        onNextPage
      }) => {
        setLoading(loading);
        if (error) return setSearchResult(null);
        if (!loading) {
          setMyOnNextCommunityPage(() => hasNextPage ? onNextPage : null);
          setSearchResult(data);
        }
      });
      return () => unsubscribeCommunity();
    } else if (searchType === _enumTabName.TabName.Communities) {
      const unsubscribeCommunity = _tsSdkReactNative.CommunityRepository.searchCommunities({
        displayName: searchValue,
        membership: 'all',
        limit: 20,
        sortBy: 'displayName'
      }, ({
        error,
        loading,
        data,
        hasNextPage,
        onNextPage
      }) => {
        setLoading(loading);
        if (error) return setSearchResult(null);
        if (!loading) {
          setOnNextCommunityPage(() => hasNextPage ? onNextPage : null);
          setSearchResult(data);
        }
      });
      return () => unsubscribeCommunity();
    } else if (searchType === _enumTabName.TabName.Users && (searchValue === null || searchValue === void 0 ? void 0 : searchValue.length) >= 3) {
      setSearchResult(null);
      const unsubscribeUser = _tsSdkReactNative.UserRepository.searchUserByDisplayName({
        displayName: searchValue,
        limit: 20,
        searchBy: [_tsSdkReactNative.SearchUsersByEnum.DISPLAY_NAME],
        matchType: _tsSdkReactNative.UserRepository.AmityUserSearchMatchType.PARTIAL
      }, ({
        error,
        loading,
        data,
        hasNextPage,
        onNextPage
      }) => {
        if (error) return setSearchResult(null);
        if (!loading) {
          setOnNextUserPage(() => hasNextPage ? onNextPage : null);
          setSearchResult(data);
        }
      });
      return () => unsubscribeUser();
    } else {
      setSearchResult(null);
    }
  }, [searchType, searchValue]);
  return {
    searchResult,
    isLoading,
    onNextCommunityPage,
    onNextUserPage,
    onNextMyCommunityPage
  };
};
exports.useAmityGlobalSearchViewModel = useAmityGlobalSearchViewModel;
//# sourceMappingURL=useAmityGlobalSearchViewModel.js.map