import { SportTypes, STATS_API_URL } from 'config'

import { callApi } from 'helpers'

import { Episodes } from './getMatchPlaylists'

type Response = {
  data?: Episodes,
  error?: {
    code: string,
    message: string,
  },
}

type GetStatsEventsArgs = {
  matchId: number,
  paramId: number,
  period?: number,
  playerId?: number,
  second?: number,
  sportType: SportTypes,
  teamId: number,
}

export const getStatsEvents = async ({
  matchId,
  paramId,
  period,
  playerId,
  second,
  sportType,
  teamId,
}: GetStatsEventsArgs) => {
  const config = {
    body: {
      half: period,
      match_id: matchId,
      match_second: second,
      offset_end: 6,
      offset_start: 6,
      option_id: 0,
      param_id: paramId,
      player_id: playerId,
      sport_id: sportType,
      team_id: teamId,
    },
  }

  const response: Response = await callApi({
    config,
    url: `${STATS_API_URL}/video`,
  })

  if (response.error) Promise.reject(response)

  return Promise.resolve(response.data || [])
}
