import { useEffect, useRef, useState } from 'react';
import { CategoryRepository } from '@amityco/ts-sdk-react-native';
import useAuth from '../../core/hooks/useAuth';
export const useCategories = ({
  limit = 5
} = {}) => {
  const {
    isConnected
  } = useAuth();
  const [categories, setCategories] = useState();
  const [loading, setLoading] = useState(true);
  const [hasMore, setHasMore] = useState(false);
  const [error, setError] = useState(null);
  const [onNextCategoryPage, setOnNextCategoryPage] = useState(null);
  const unsubscribersRef = useRef([]);
  const fetchCategories = () => {
    setLoading(true);
    return CategoryRepository.getCategories({
      limit
    }, ({
      error,
      loading,
      data,
      hasNextPage,
      onNextPage
    }) => {
      if (error) setError(error);
      if (!loading) {
        setLoading(loading);
        setCategories(data);
        setHasMore(hasNextPage);
        setOnNextCategoryPage(() => {
          if (hasNextPage) return onNextPage;
          return null;
        });
      }
    });
  };
  const unsubscribeListener = () => {
    unsubscribersRef.current.forEach(unsubscriber => unsubscriber());
    unsubscribersRef.current = [];
  };
  const refresh = () => {
    unsubscribeListener();
    setError(null);
    const unsubscribe = fetchCategories();
    unsubscribersRef.current.push(unsubscribe);
  };
  useEffect(() => {
    if (!isConnected) return () => {};
    const unsubscribe = fetchCategories();
    unsubscribersRef.current.push(unsubscribe);
    return () => unsubscribeListener();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [isConnected]);
  return {
    refresh,
    categories,
    hasMore,
    onNextCategoryPage,
    loading,
    error
  };
};
//# sourceMappingURL=useCategories.js.map