import map from 'lodash/map'

import format from 'date-fns/format'

import type { Match } from 'requests'

import { parseDate } from 'helpers/parseDate'

import { getMatchAccess } from './getMatchClickAction'

const prepareMatch = (match: Match) => {
  const {
    calc,
    country_id,
    date: matchDate,
    has_video,
    id,
    live,
    preview,
    sport,
    storage,
    team1,
    team2,
    tournament,
  } = match
  const date = parseDate(matchDate)
  return {
    access: getMatchAccess(match),
    calc,
    countryId: country_id,
    date,
    formattedDate: format(date, 'dd.MM.yy'),
    hasVideo: has_video,
    id,
    live,
    preview,
    sportType: sport,
    storage,
    team1,
    team2,
    time: format(date, 'HH:mm'),
    tournament,
  }
}

export const prepareMatches = (matches: Array<Match>) => map(
  matches,
  prepareMatch,
)
