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

const proc = PROCEDURES.save_user_custom_subscription

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

type Response = {
  _p_error: string | null,
  _p_status: 1 | 2,
}

type Args = {
  action: SubscriptionAction,
  id: number,
  sport: SportTypes,
  type: ProfileTypes,
}

const responseStatus = {
  FAILURE: 2,
  SUCCESS: 1,
}

export const saveUserCustomSubscription = async ({
  action,
  id,
  sport,
  type,
} : Args) => {
  const config = {
    body: {
      params: {
        _p_action: action,
        _p_id: id,
        _p_sport: sport,
        _p_type: type,
      },
      proc,
    },
  }

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

  if (response._p_status === responseStatus.SUCCESS) {
    return Promise.resolve()
  }
  return Promise.reject(response._p_error)
}
