import { STATS_API_URL, PROCEDURES } from 'config'

import { Episode, Episodes } from 'requests'

import { callApi } from 'helpers'

const proc = PROCEDURES.get_match_plays

type Args = {
  matchId: number,
  sportType: number,
}

type PlayerNames = {
  name_eng: string,
  name_rus: string,
  num?: number,
}

export type Event = Episode & {
  /** clear time */
  c: string,

  /** event lexic */
  l: number,

  /** like */
  like?: boolean,

  /** event id */
  n: number,

  /** player id */
  p?: number,

  /** player names */
  pl?: PlayerNames,

  /** repeat */
  rep?: Episodes,

  /** score team1 */
  sc1?: number,

  /** score team2 */
  sc2?: number,

  /** team id */
  t?: number,
}

export type Events = Array<Event>

type Response = {
  data?: Events,
}

export const getMatchEvents = async ({
  matchId,
  sportType,
}: Args) => {
  const config = {
    body: {
      params: {
        match_id: matchId,
        sport_id: sportType,
      },
      proc,
    },
  }

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

  if (!response?.data) return Promise.reject(response)

  return response?.data || []
}
