import { DATA_URL, PROCEDURES } from 'config'
import { callApi } from 'helpers'

const proc = PROCEDURES.create_user

const responseStatus = {
  FAILURE: 2,
  SUCCESS: 1,
}

type Response = {
  _p_error: string | null,
  _p_status: 1 | 2,
}

type Args = {
  email: string,
  firstname: string,
  lastname: string,
  password: string,
}

export const register = async ({
  email,
  firstname,
  lastname,
  password,
}: Args) => {
  const config = {
    body: {
      params: {
        _p_email: email,
        _p_firstname: firstname,
        _p_lastname: lastname,
        _p_password: password,
      },
      proc,
    },
  }

  const response: Response = await callApi({
    config,
    url: DATA_URL,
  })

  if (response._p_status === responseStatus.SUCCESS) {
    return Promise.resolve(response)
  }

  return Promise.reject(response._p_error)
}
