import map from 'lodash/map'
import some from 'lodash/some'
import filter from 'lodash/filter'
import uniq from 'lodash/uniq'
import flatten from 'lodash/flatten'
import compact from 'lodash/compact'
import startsWith from 'lodash/startsWith'
import toLower from 'lodash/toLower'

import { Match } from 'requests'

const checkStartString = (text: string, searchString: string) => startsWith(
  toLower(text), toLower(searchString),
)
export const getAge = (filterList: Array<Match>) => compact(
  uniq(
    flatten(
      map(
        filterList, (item) => [item.team1.youth_age, item.team2.youth_age],
      ),
    ),
  ),
)

export const getDivision = (filterList: Array<Match>) => filter(
  flatten(
    map(
      filterList, (item) => [item.team1.division, item.team2.division],
    ),
  ), (item) => item?.id,
)

export const getMainTeam = (filterList: Array<Match>) => filter(
  flatten(
    map(
      filterList, (item) => [item.team1.main_team, item.team2.main_team],
    ),
  ), (item) => item?.id,
)

export const getArena = (filterList: Array<Match>, inputValue: string) => filter(
  map(
    filterList, (item) => item.arena,
  ), (item) => item?.id
    && (checkStartString(item.name_eng, inputValue)
      || checkStartString(item.name_rus, inputValue)),
)

export const getRound = (filterList: Array<Match>) => filter(
  map(
    filterList, (item) => item.round,
  ), (item) => item?.id,
)

export const getGender = (filterList: Array<Match>) => {
  const list = []

  if (some(filterList, (item) => item.team1.gender === 1 || item.team2.gender === 1)) list.push({ id: 1, title: 'gender_male_long' })
  if (some(filterList, (item) => item.team1.gender === 2 || item.team2.gender === 2)) list.push({ id: 2, title: 'gender_female_long' })

  return list
}
