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

import { client } from 'config/clients'

import { addSportType } from 'features/Matches/helpers/addSportType'

import type { MatchesBySection } from './types'
import { getMatchesPreviews } from './getPreviews'
import { requestMatches } from './request'

const proc = PROCEDURES.get_tournament_matches

type Args = {
  limit: number,
  offset: number,
  sportType: SportTypes,
  tournamentId: number,
}

export const getTournamentMatches = async ({
  limit,
  offset,
  sportType,
  tournamentId,
}: Args): Promise<MatchesBySection> => {
  const url = `${API_ROOT}/v1/data/get-tournament-matches`

  const config = {
    body: {
      limit,
      offset,
      sport: sportType,
      tournament_id: tournamentId,
      ...client.requests?.[proc],
    },
  }

  return requestMatches(config, url)
    .then((matches) => addSportType(matches, sportType))
    .then(getMatchesPreviews)
}
