import { PROCEDURES, API_ROOT } from 'config'

import { client } from 'config/clients'

import type { MatchesBySection } from './types'
import { requestMatches } from './request'
import { getMatchesPreviews } from './getPreviews'

const proc = PROCEDURES.get_matches

type Args = {
  date: string,
  limit: number,
  offset: number,
  timezoneOffset: number,
}

export const getHomeMatches = async ({
  date,
  limit,
  offset,
  timezoneOffset,
}: Args): Promise<MatchesBySection> => {
  const url = `${API_ROOT}/v1/data/matches/query`

  const config = {
    body: {
      date: `${date} 00:00:00`,
      gmt: timezoneOffset,
      limit,
      offset,
      ...client.requests?.[proc],
    },
  }

  return requestMatches(config, url)
    .then(getMatchesPreviews)
}
