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

import type { SubscriptionResponse } from 'requests/getSubscriptions'

import { handleUnsuccessfulSubscription } from './handleUnsuccessfulSubscription'

type Subscription = {
  cardId: string,
  item: SubscriptionResponse,
}

export const buyMatchSubscription = ({ cardId, item }: Subscription) => {
  const config = {
    body: {
      card_id: cardId,
      item,
    },
  }

  return callApi({
    config,
    url: `${API_ROOT}/account/subscription`,
  }).catch(handleUnsuccessfulSubscription)
}
