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

const proc = PROCEDURES.get_objects

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

export type NamedObject = {
  id: number,
  name_eng: string,
  name_rus: string,
}

export type PlayerTypeFromSearch = {
  country?: NamedObject,
  firstname_eng: string,
  firstname_rus: string,
  gender?: Gender,
  id: number,
  lastname_eng: string,
  lastname_rus: string,
  sport: SportTypes,
  team?: NamedObject,
}

export type PlayersType = Array<PlayerTypeFromSearch>

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

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

export type SearchItems = {
  players?: PlayersType,
  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,
  })
}
