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

import type { SubscriptionResponse } from '../getSubscriptions'
import { handleUnsuccessfulSubscription } from './handleUnsuccessfulSubscription'

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

export const buyMatchPayOnceSubscription = ({ cardId, item }: Args) => {
  const config = {
    body: {
      card_id: cardId,
      item,
    },
  }

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