import { API_ROOT } from 'config'

import { callApi } from 'helpers'

import type { SubscriptionResponse } from 'requests/getSubscriptions'

type Props = {
  action: 'one_payment' | 'create_subscription',
  item: SubscriptionResponse,
  product_name: string,
  service: string,
  url_cancel?: string,
  url_return?: string | null,
}

export const getPaymentUrl = async ({
  action,
  item,
  product_name,
  service,
  url_cancel,
  url_return,
}: Props) => {
  const config = {
    body: {
      action,
      data: {
        item: { ...item, product_name },
        url_cancel,
        url_return,
      },
      service,
    },
  }

  return callApi({
    config,
    url: `${API_ROOT}/account/payments`,
  })
}
