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,
  playerId: number,
  sportType: SportTypes,
}

export const getPlayerMatches = async ({
  limit,
  offset,
  playerId,
  sportType,
}: Args): Promise<MatchesBySection> => {
  const config = {
    body: {
      params: {
        _p_limit: limit,
        _p_offset: offset,
        _p_player_id: playerId,
        _p_sport: sportType,
        ...client.requests?.[proc],
      },
      proc,
    },
  }

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