import map from 'lodash/map'

import format from 'date-fns/format'

import type { Match, Team } from 'requests'
import { ProfileTypes, SportTypes } from 'config'
import { getProfileLogo, getSportLexic } from 'helpers'

type Name = 'name_rus' | 'name_eng'

type Args = {
  sport: SportTypes,
  suffix: string,
  team: Team,
}

const prepareTeam = ({
  sport,
  suffix,
  team,
}: Args) => ({
  logo: getProfileLogo({
    id: team.id,
    profileType: ProfileTypes.TEAMS,
    sportType: sport,
  }),
  name: team[`name_${suffix}` as Name],
  score: team.score,
})

const prepareMatch = ({
  date,
  id,
  sport,
  stream_status,
  team1,
  team2,
  tournament,
}: Match, suffix: string) => ({
  date: format(new Date(date), 'dd.MM.yy'),
  id,
  preview: '/images/preview.png',
  sportName: getSportLexic(sport),
  sportType: sport,
  streamStatus: stream_status,
  team1: prepareTeam({
    sport,
    suffix,
    team: team1,
  }),
  team2: prepareTeam({
    sport,
    suffix,
    team: team2,
  }),
  time: format(new Date(date), 'HH:mm'),
  tournamentName: tournament?.[`name_${suffix}` as Name],
})

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