import { ProfileTypes } from 'config'
import map from 'lodash/map'

import type { SearchItems } from 'requests'

export type NormalizedSearchResults = ReturnType<typeof normalizeItems>

export const normalizeItems = (searchItems: SearchItems) => {
  const players = map(searchItems.players, (player) => ({
    ...player,
    additionalInfo: {
      ...player.team,
      id: player.country?.id,
    },
    name_eng: `${player.firstname_eng} ${player.lastname_eng}`,
    name_rus: `${player.firstname_rus} ${player.lastname_rus}`,
    profileType: ProfileTypes.PLAYERS,
  }))

  const teams = map(searchItems.teams, (team) => ({
    ...team,
    additionalInfo: team.country,
    profileType: ProfileTypes.TEAMS,
  }))

  const tournaments = map(searchItems.tournaments, (tournament) => ({
    ...tournament,
    additionalInfo: tournament.country,
    profileType: ProfileTypes.TOURNAMENTS,
  }))

  return {
    [ProfileTypes.PLAYERS]: players,
    [ProfileTypes.TEAMS]: teams,
    [ProfileTypes.TOURNAMENTS]: tournaments,
  }
}
