import isEmpty from 'lodash/isEmpty'
import filter from 'lodash/filter'

import { API_ROOT } from 'config'
import { callApi } from 'helpers'

const filterByIds = (videos: Videos) => {
  const zeroIdVideos = filter(videos, { abc: '0' })
  return isEmpty(zeroIdVideos) ? videos : zeroIdVideos
}

export type Video = {
  /** id дорожки */
  abc: string,
  duration: number,
  period: number,
  quality: string,
  start_ms: number,
  url: string,
}

export type Videos = Array<Video>

export const getVideos = (
  sportType: number,
  matchId: number,
): Promise<Videos> => {
  const config = {
    body: {
      match_id: matchId,
      sport_id: sportType,
    },
  }

  return callApi({
    config,
    // эндпоинт удалили со стейджинга, временно ссылаемся на прод
    url: `${API_ROOT}/videoapi`,
  }).then(filterByIds)
}
