import map from 'lodash/map'

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

import type { SubscriptionType } from 'features/BuyMatchPopup/types'

type Subscription = {
  id: number,
  type: SubscriptionType,
}

const buyMatchSubscription = ({ id, type }: Subscription) => {
  const config = {
    body: {
      interval: type,
      is_scheduled: 0,
      subscription_plan: id,
    },
  }

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

export const buyMatchSubscriptions = (subscriptions: Array<Subscription>) => (
  Promise.all(map(subscriptions, buyMatchSubscription))
)
