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_team_matches

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

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

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

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