import { getRequest, postRequest } from '.';
import { RegisterUser, User } from './account.types';

export async function login(email: string, password: string): Promise<User | undefined> {
  const response = await postRequest('account/login', { email, password });
  if (response?.status === 200) {
    return response.json();
  } else {
    return undefined;
  }
}

export async function register(user: RegisterUser): Promise<boolean> {
  const response = await postRequest('account/register', user);
  if (response?.status === 201) {
    return true;
  } else {
    return false;
  }
}

export async function logout() {
  return await postRequest('account/logout', {});
}

export async function refresh(): Promise<User | undefined> {
  const response = await getRequest('account/session');
  if (response?.status === 200) {
    return response.json();
  } else {
    return undefined;
  }
}
