import { 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 config = {
    body: {
      params: {
        _p_limit: limit,
        _p_offset: offset,
        _p_sport: sportType,
        _p_tournament_id: tournamentId,
        ...client.requests?.[proc],
      },
      proc,
    },
  }

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