import { useQueries } from '@tanstack/react-query'
import { getMovieFull } from '../api/tmdb'

const STALE = 7 * 24 * 60 * 60 * 1000

/**
 * Resolve a list of TMDB movie ids into their TMDB-shaped objects in
 * the original order, dropping entries TMDB doesn't recognise. Used by
 * the home page's "Discover canon" rows so the bundled lists in
 * `canon-lists.ts` can render through the same TMDB->BaseItemDto
 * mapping path as everything else.
 */
export function useCanonListResolved(ids: number[]) {
  const queries = useQueries({
    queries: ids.map(id => ({
      queryKey: ['tmdb', 'movie-full', id],
      queryFn: () => getMovieFull(id),
      staleTime: STALE,
    })),
  })
  const results: any[] = []
  for (let i = 0; i < ids.length; i++) {
    const data = queries[i]?.data
    if (data) {
      results.push({ ...data, media_type: 'movie' })
    }
  }
  return {
    items: results,
    isLoading: queries.some(q => q.isLoading),
  }
}
