import isEmpty from 'lodash/isEmpty'

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

import { callApi, getSportLexic } from 'helpers'

import type { Settings } from 'features/MatchPopup/types'
import { defaultSettings } from 'features/MatchPopup/types'

import type { Episodes } from './getMatchPlaylists'

const proc = PROCEDURES.ott_match_popup_player_playlist

type Args = {
  matchId: number,
  playerId: number,
  settings?: Settings,
  sportType: SportTypes,
}

type Response = {
  data?: Episodes,
}

export const getPlayerPlaylists = async ({
  matchId,
  playerId,
  settings = defaultSettings,
  sportType,
}: Args) => {
  const actions = isEmpty(settings.selectedActions) ? null : settings.selectedActions

  const config = {
    body: {
      params: {
        _p_actions: actions,
        _p_match_id: matchId,
        _p_offset_end: settings.episodeDuration.after,
        _p_offset_start: -settings.episodeDuration.before,
        _p_player_id: playerId,
        _p_type: settings.selectedFormat,
      },
      proc,
    },
  }

  const response: Response = await callApi({
    config,
    url: `${DATA_URL}/${getSportLexic(sportType)}`,
  })

  return response?.data || []
}
