import { useCallback, useState } from 'react'

import type { Events } from 'requests'
import { getMatchEvents } from 'requests'

import { usePageParams } from 'hooks/usePageParams'

import { useEventsLexics } from './useEventsLexics'

export const useEvents = () => {
  const [events, setEvents] = useState<Events>([])
  const { fetchLexics } = useEventsLexics()
  const { profileId: matchId, sportType } = usePageParams()

  const fetchMatchEvents = useCallback(() => {
    getMatchEvents({
      matchId,
      sportType,
    }).then(fetchLexics)
      .then(setEvents)
      .catch(() => setEvents([]))
  }, [
    fetchLexics,
    matchId,
    sportType,
  ])

  return { events, fetchMatchEvents }
}
