import { 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 config = {
    body: {
      params: {
        _p_limit: limit,
        _p_match_completed: p_match_completed,
        _p_offset: offset,
        _p_player_id: playerId,
        _p_sport: sportType,
        _p_sub_only: sub_only,
        ...client.requests?.[proc],
      },
      proc,
    },
  }

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