import isUndefined from 'lodash/isUndefined'

import { callApi } from 'helpers'

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

export type TeamStatItem = {
  lexic: number,
  name_en: string,
  name_ru: string,
  order: number,
  param1: Param,
  param2: Param | null,
}

type Response = {
  data?: {
    [teamId: string]: Array<TeamStatItem>,
  },
  error?: string,
  message?: string,
}

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

export const getTeamsStats = async ({
  matchId,
  second,
  sportName,
}: GetTeamsStatsArgs) => {
  const config = {
    method: 'GET',
  }

  const response: Response = await callApi({
    config,
    url: `http://136.243.17.103:8888/${sportName}/matches/${matchId}/teams/stats?group_num=0${isUndefined(second) ? '' : `&second=${second}`}`,
  })

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

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