// const host = "https://hi-po.com.cn";
const host = "";

export async function sendCaptcha(phone: string) {
  const formData = new FormData();
  formData.append("phone", phone);

  const result = await fetch(`${host}/api/v1/user/sendCaptcha`, {
    method: "POST",
    body: formData,
  });
  const json = await result.json();
  return json;
}

type LoginParams = {
  type?: number;
  username?: string;
  password?: string;
  captcha?: string;
  sourceId?: number;
};

export async function login(params: LoginParams) {
  const formData = new FormData();
  Object.entries(params).forEach(([key, value]) => {
    if (value !== undefined) {
      formData.append(key, value.toString());
    }
  });
  const result = await fetch(`${host}/api/v1/user/login`, {
    method: "POST",
    body: formData,
  });
  const json = await result.json();
  return json;
}

type RegisterParams = {
  phone?: string;
  password?: string;
  inviteCode?: string;
  captcha?: string;
};

export async function register(params: RegisterParams) {
  const formData = new FormData();
  Object.entries(params).forEach(([key, value]) => {
    if (value !== undefined) {
      formData.append(key, value.toString());
    }
  });
  const result = await fetch(`${host}/api/v1/user/register`, {
    method: "POST",
    body: formData,
  });
  const json = await result.json();
  return json;
}

export async function userInfo() {
  const options = {
    method: "POST",
  };
  const response = await fetch(`${host}/api/v1/user/userInfo`, options);
  const json = await response.json();
  return json;
}
