import { useInfiniteQuery } from '@tanstack/react-query';
import { getFollowingFeed } from '../dal';
import { feedKeys } from '../feedKeys';
import { UrlType, ActivitySource, ActivityType } from '@semble/types';

interface Props {
  limit?: number;
  urlType?: UrlType;
  source?: ActivitySource;
  activityTypes?: ActivityType[];
  includeKnownBots?: boolean;
  enabled?: boolean;
}

export default function useFollowingFeed(props?: Props) {
  const limit = props?.limit ?? 15;
  const enabled = props?.enabled ?? true;

  const query = useInfiniteQuery({
    queryKey: feedKeys.followingInfinite(
      limit,
      props?.urlType,
      props?.source,
      props?.activityTypes,
      props?.includeKnownBots,
    ),
    staleTime: 10000,
    initialPageParam: 1,
    refetchOnWindowFocus: false,
    enabled,
    queryFn: ({ pageParam = 1 }) => {
      return getFollowingFeed({
        limit,
        page: pageParam,
        urlType: props?.urlType,
        source: props?.source,
        activityTypes: props?.activityTypes,
        includeKnownBots: props?.includeKnownBots,
      });
    },
    getNextPageParam: (lastPage) => {
      if (lastPage.pagination.hasMore) {
        return lastPage.pagination.currentPage + 1;
      }
      return undefined;
    },
  });

  return query;
}
