import { callApi } from 'helpers'

import { ADS_API_URL } from 'config'
import { DeviceType } from '../../components/Ads/types'

export type AdsParams = {
  client_type: DeviceType,
  language: string,
  type_id: number,
}

export type AdType = {
  'duration': number,
  'frequency': number,
  'id': number,
  'impressions': number,
  'link': string,
  'media': {
    url: string,
  },
  'name': string,
  'position': {
    'id': number,
    'name_eng': string,
    'name_rus': string,
    'source_type': string,
  },
  remaining_views: number,
  'time_close': number,
  'type': {
    'id': number,
    'name_eng': string,
    'name_rus': string,
  },
}

export type PositionName = 'header' | 'block' | 'match_cell' | 'mobile'

export type AdResponse = Record<PositionName, AdsListType>

export type AdsListType = Array<AdType>

export const getAds = (params: AdsParams): Promise<AdResponse> => {
  const config = {
    body: params,
  }

  return callApi({
    config,
    url: ADS_API_URL,
  })
}
