import {
  API_ROOT,
  ProfileTypes,
  SportTypes,
} from 'config'
import { callApi } from 'helpers'

const profiles = {
  [ProfileTypes.TEAMS]: 'team',
  [ProfileTypes.TOURNAMENTS]: 'tournament',
  [ProfileTypes.PLAYERS]: 'team',
  [ProfileTypes.MATCHES]: '',
  [ProfileTypes.SUPERTOURNAMENTS]: '',
}

type Response = {
  b: number,
  code: string,
  g: number,
  r: number,
}

type Args = {
  profileId: number,
  profileType: ProfileTypes,
  sportType: SportTypes,
}

export const getProfileColor = async ({
  profileId,
  profileType,
  sportType,
}: Args): Promise<string> => {
  const config = {
    body: {
      profile_id: profileId,
      profile_type: profiles[profileType],
      sport_id: sportType,
    },
  }

  const response: Response = await callApi({
    config,
    url: `${API_ROOT}/profile/color`,
  })

  return `rgba(${response.r}, ${response.g}, ${response.b}, 0.56)`
}
