import { ApiError, apiPost } from "../../lib/api";
import type { AuthResponse, LoginRequest } from "../../types/auth";

export async function login(req: LoginRequest): Promise<AuthResponse> {
  return apiPost<AuthResponse, LoginRequest>("/api/auth/login", req);
}

export async function register(req: LoginRequest): Promise<void> {
  try {
    await apiPost<void, LoginRequest>("/api/auth/register", req);
  } catch (err: unknown) {
    if (err instanceof ApiError && err.status === 409) {
      throw new Error("User already exists. Please log in instead.");
    }
    throw err;
  }
}
