"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _react = require("react");
const MIN_SEARCH_LENGTH = 3;
const useSearch = (searchText, privateCommunityId = '') => {
  const onNextPageRef = (0, _react.useRef)(null);
  const [searchResult, setSearchResult] = (0, _react.useState)([]);
  const searchPrivateCommunityMember = (0, _react.useCallback)(text => {
    return _tsSdkReactNative.CommunityRepository.Membership.searchMembers({
      communityId: privateCommunityId,
      ...(text ? {
        search: text
      } : {}),
      limit: 5,
      sortBy: 'firstCreated',
      memberships: ['member'],
      includeDeleted: false
    }, ({
      data,
      error,
      hasNextPage,
      onNextPage
    }) => {
      if (error) return null;
      onNextPageRef.current = hasNextPage ? onNextPage : null;
      const mappedSearchData = data.map(item => {
        return {
          ...item.user,
          name: item.user.displayName,
          id: item.userId
        };
      });
      setSearchResult(mappedSearchData);
    });
  }, [privateCommunityId]);
  const getAllUsers = (0, _react.useCallback)(() => {
    return _tsSdkReactNative.UserRepository.getUsers({
      limit: 5
    }, ({
      data,
      error,
      hasNextPage,
      onNextPage
    }) => {
      if (error) return null;
      hasNextPage ? onNextPageRef.current = onNextPage : onNextPageRef.current = null;
      const mappedSearchData = data.map(item => {
        return {
          ...item,
          name: item.displayName,
          id: item.userId
        };
      });
      setSearchResult(mappedSearchData);
    });
  }, []);
  const searchAllUsers = (0, _react.useCallback)(text => {
    return _tsSdkReactNative.UserRepository.searchUserByDisplayName({
      displayName: text,
      limit: 5,
      searchBy: [_tsSdkReactNative.SearchUsersByEnum.DISPLAY_NAME],
      matchType: _tsSdkReactNative.UserRepository.AmityUserSearchMatchType.PARTIAL
    }, ({
      data,
      error,
      hasNextPage,
      onNextPage
    }) => {
      if (error) return null;
      hasNextPage ? onNextPageRef.current = onNextPage : onNextPageRef.current = null;
      const mappedSearchData = data.map(item => {
        return {
          ...item,
          name: item.displayName,
          id: item.userId
        };
      });
      setSearchResult(mappedSearchData);
    });
  }, []);
  (0, _react.useEffect)(() => {
    if (searchText == null) {
      onNextPageRef.current = null;
      setSearchResult([]);
      return () => {};
    }

    // Treat 1–2 character queries as empty: show all users without filtering.
    // Only send a search query once the user has typed at least MIN_SEARCH_LENGTH chars.
    const effectiveText = searchText.length > 0 && searchText.length < MIN_SEARCH_LENGTH ? '' : searchText;
    let unsubscribe;
    if (privateCommunityId) {
      unsubscribe = searchPrivateCommunityMember(effectiveText) ?? undefined;
    } else if (!effectiveText) {
      unsubscribe = getAllUsers() ?? undefined;
    } else {
      unsubscribe = searchAllUsers(effectiveText) ?? undefined;
    }
    return () => {
      var _unsubscribe;
      (_unsubscribe = unsubscribe) === null || _unsubscribe === void 0 || _unsubscribe();
    };
  }, [getAllUsers, privateCommunityId, searchAllUsers, searchPrivateCommunityMember, searchText]);
  return {
    searchResult,
    getNextPage: onNextPageRef.current
  };
};
var _default = exports.default = useSearch;
//# sourceMappingURL=useSearch.js.map