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

import { Episode, Episodes } from 'requests'

import { callApi, getSportLexic } from 'helpers'

const proc = PROCEDURES.ott_match_events

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

type PlayerNames = {
  name_eng: string,
  name_rus: string,
}

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: {
        _p_match_id: matchId,
      },
      proc,
    },
  }

  const response: Response = await callApi({
    config,
    url: `${DATA_URL}/${getSportLexic(sportType)}`,
  })

  return response?.data || []
}
