import isUndefined from 'lodash/isUndefined'

import { callApi } from 'helpers'

import { STATS_API_URL } from 'config'

export type PlayerParam = {
  clickable: boolean,
  data_type: string,
  id: number,
  lexic: number,
  lexica_short: number | null,
  markers: Array<number> | null,
  name_en: string,
  name_ru: string,
  val: number | null,
}

export type PlayersStats = {
  [playerId: string]: {
    [paramId: string]: PlayerParam,
  },
}

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

type GetPlayersStatsArgs = {
  matchId: number,
  second?: number,
  sportName: string,
  teamId: number,
}

export const getPlayersStats = async ({
  matchId,
  second,
  sportName,
  teamId,
}: GetPlayersStatsArgs) => {
  const config = {
    method: 'GET',
  }

  const response: Response = await callApi({
    config,
    url: `${STATS_API_URL}/${sportName}/matches/${matchId}/teams/${teamId}/players/stats${isUndefined(second) ? '' : `?second=${second}`}`,
  })

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

  return Promise.resolve(response.data || {})
}
