import {
  DATA_URL,
  PROCEDURES,
  SportTypes,
} from 'config'
import { callApi } from 'helpers'

const proc = PROCEDURES.get_objects

export enum Gender {
  MALE = 1,
  FEMALE = 2,
}

type Player = {
  firstname_eng: string,
  firstname_rus: string,
  gender?: Gender,
  id: number,
  lastname_eng: string,
  lastname_rus: string,
  sport: SportTypes,
  team?: {
    id: number,
    name_eng: string,
    name_rus: string,
  },
}

type Team = {
  country?: {
    id: number,
    name_eng: string,
    name_rus: string,
  },
  gender?: Gender,
  id: number,
  name_eng: string,
  name_rus: string,
  sport: SportTypes,
}

type Tournament = {
  country?: {
    id: number,
    name_eng: string,
    name_rus: string,
  },
  gender?: Gender,
  id: number,
  name_eng: string,
  name_rus: string,
  sport: SportTypes,
}

export type SearchItems = {
  players?: Array<Player>,
  teams?: Array<Team>,
  tournaments?: Array<Tournament>,
}

export const getSearchItems = (
  searchString: string,
  abortSignal: AbortSignal,
): Promise<SearchItems> => {
  const config = {
    body: {
      params: {
        _p_name: searchString,
      },
      proc,
    },
  }

  return callApi({
    abortSignal,
    config,
    url: DATA_URL,
  })
}
