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

import { UserFavorites } from './getUserSportFavs'

const proc = PROCEDURES.save_user_favorite

enum FavoriteStatusTypes {
  SUCCESS = 1,
  FAILURE = 2,
}

export enum FavoritesActions {
  ADD = 1,
  REMOVE = 2,
}

type Response = {
  _p_data: UserFavorites,
  _p_status: FavoriteStatusTypes,
}

type Args = {
  action: FavoritesActions,
  id: number,
  isAuto?: boolean,
  sport: SportTypes,
  type: ProfileTypes,
}

// при добавлении дубликата back возвращает {}
export const modifyUserFavorites = async ({
  action,
  id,
  isAuto = false,
  sport,
  type,
}: Args) => {
  const config = {
    body: {
      params: {
        _p_action: action,
        _p_id: id,
        _p_is_auto_add: isAuto,
        _p_sport: sport,
        _p_type: type,
      },
      proc,
    },
  }

  const { _p_data, _p_status }: Response = await callApi({
    config,
    url: DATA_URL,
  })

  if (_p_status === FavoriteStatusTypes.FAILURE) {
    return Promise.reject()
  }
  return Promise.resolve(_p_data)
}
