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 { requestMatches } from './request'
import { getMatchesPreviews } from './getPreviews'

const proc = PROCEDURES.get_player_matches

type Args = {
  limit: number,
  offset: number,
  p_match_completed?: boolean,
  playerId: number,
  sportType: SportTypes,
  sub_only?: boolean,
}

export const getPlayerMatches = async ({
  limit,
  offset,
  p_match_completed,
  playerId,
  sportType,
  sub_only,
}: Args): Promise<MatchesBySection> => {
  const url = `${API_ROOT}/v1/data/get-player-matches`

  const config = {
    body: {
      limit,
      match_completed: p_match_completed,
      offset,
      player_id: playerId,
      sport: sportType,
      sub_only,
      ...client.requests?.[proc],
    },
  }

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