import {
  DATA_URL,
  PROCEDURES,
  SportTypes,
} from 'config'

import { callApi } from 'helpers'

import { TournamentType, SportInfo } from './getMatches/types'

const proc = PROCEDURES.get_match_info

export type Team = {
  abbrev_eng: string,
  abbrev_rus: string,
  id: number,
  name_eng: string,
  name_rus: string,
  score: number,
}

export type MatchTournament = {
  id: number,
  name_eng: string,
  name_rus: string,
  sportType: SportTypes,
}

export type VideoBound = {
  e: string,
  h: string,
  s: string,
}

export type VideoBounds = Array<VideoBound>

export type MatchInfo = {
  access?: boolean,
  calc: boolean,
  country: TournamentType,
  country_id: number,
  date: string,
  has_hls?: boolean,
  has_video: boolean,
  live: boolean,
  playbackUrl?: string,
  sport: SportInfo,
  storage: boolean,
  sub: boolean,
  team1: Team,
  team2: Team,
  tournament: MatchTournament,
  video_bounds?: VideoBounds,
  youtube_link?: string,
} | null

export const getMatchInfo = (sportId: number, matchId: number): Promise<MatchInfo> => {
  const config = {
    body: {
      params: {
        _p_match_id: matchId,
        _p_sport: sportId,
      },
      proc,
    },
  }

  return callApi({
    config,
    url: DATA_URL,
  })
}
