import {
  asLoginPayload,
  wasChangeVouchersPayload
} from '../../types/server-cleaners'


import { applyLoginPayload, makeAuthJson } from './login'
import { loginFetch } from './login-fetch'
import { getStashById } from './login-selectors'
import { saveStash } from './login-stash'


/**
 * Approves or rejects vouchers on the server.
 */
export async function changeVoucherStatus(
  ai,
  login,
  vouchers
) {
  const { stashTree } = getStashById(ai, login.loginId)
  const reply = await loginFetch(ai, 'POST', '/v2/login/vouchers', {
    ...makeAuthJson(stashTree, login),
    data: wasChangeVouchersPayload(vouchers)
  })
  const newStashTree = applyLoginPayload(
    stashTree,
    login.loginKey,
    asLoginPayload(reply)
  )
  return await saveStash(ai, newStashTree)
}
