import { infiniteQueryOptions, queryOptions } from '@tanstack/react-query'
import { getStoryCarousel, getStoryViewers } from 'src/lib/api'
import type { LinkPaginatedResponse } from 'src/lib/pagination'
import { queryKeys } from './keys'

export const storyQueries = {
  carousel: () =>
    queryOptions({
      queryKey: queryKeys.stories.carousel(),
      queryFn: getStoryCarousel,
      staleTime: 2 * 60 * 1000,
      gcTime: 30 * 60 * 1000,
    }),

  viewers: (id: string) =>
    infiniteQueryOptions({
      queryKey: queryKeys.stories.viewers(id),
      queryFn: ({ pageParam }) => getStoryViewers(id, pageParam),
      initialPageParam: null as string | null,
      staleTime: 60 * 1000,
      gcTime: 10 * 60 * 1000,
      getNextPageParam: (lastPage: LinkPaginatedResponse<unknown>) =>
        lastPage.nextPage ?? undefined,
    }),
}
