import { useContext } from 'react'
import { NetworkRequest, useNetwork } from '../components/useNetwork'
import { ArtistContext } from '../contexts/ArtistContext'
import { UserContext } from '../contexts/UserContext'
import { Track } from '../types/Track'

export interface TrackResponse {
  track: Track
}

export const cachedTrackPaths: Set<string> = new Set()

export function useTrack(
  isrc: string,
  onRetry?: () => void
): NetworkRequest<TrackResponse> {
  const user = useContext(UserContext)
  const artist = useContext(ArtistContext)
  const path = `api/artists/${artist.sonyArtistId}/track/${isrc}`
  cachedTrackPaths.add(path)
  return useNetwork<TrackResponse>(path, user, onRetry)
}
