export type RequestOptions = { baseUrl?: string; headers?: HeadersInit };
type ApiEnvelope = { data?: unknown; error?: { message?: string } };
const request = async (
  method: string,
  path: string,
  body: unknown,
  options: RequestOptions = {},
) => {
  const response = await globalThis.fetch((options.baseUrl || "") + path, {
    method,
    headers: { "content-type": "application/json", ...(options.headers || {}) },
    body: body === undefined ? undefined : JSON.stringify(body),
  });
  const value: ApiEnvelope = await response.json();
  if (!response.ok)
    throw Object.assign(new Error(value?.error?.message || "API request failed"), {
      status: response.status,
      body: value,
    });
  return value.data ?? value;
};
export const postWebhooksPayment = (
  params: Record<string, string> = {},
  body: unknown,
  options: RequestOptions = {},
) => {
  void params;
  return request("POST", `/api/webhooks/payment`, body, options);
};
export const getUsersCsr = (
  params: Record<string, string> = {},
  body: unknown,
  options: RequestOptions = {},
) => {
  void params;
  return request("GET", `/api/users/csr`, body, options);
};
export const getUsersSsr = (
  params: Record<string, string> = {},
  body: unknown,
  options: RequestOptions = {},
) => {
  void params;
  return request("GET", `/api/users/ssr`, body, options);
};
export const postGraphqlExample = (
  params: Record<string, string> = {},
  body: unknown,
  options: RequestOptions = {},
) => {
  void params;
  return request("POST", `/api/graphql-example`, body, options);
};
export const postTypedUser = (
  params: Record<string, string> = {},
  body: unknown,
  options: RequestOptions = {},
) => {
  void params;
  return request("POST", `/api/typed-user`, body, options);
};
export const postLogout = (
  params: Record<string, string> = {},
  body: unknown,
  options: RequestOptions = {},
) => {
  void params;
  return request("POST", `/api/logout`, body, options);
};
export const getHello = (
  params: Record<string, string> = {},
  body: unknown,
  options: RequestOptions = {},
) => {
  void params;
  return request("GET", `/api/hello`, body, options);
};
export const postLogin = (
  params: Record<string, string> = {},
  body: unknown,
  options: RequestOptions = {},
) => {
  void params;
  return request("POST", `/api/login`, body, options);
};
export const getEcho = (
  params: Record<string, string> = {},
  body: unknown,
  options: RequestOptions = {},
) => {
  void params;
  return request("GET", `/api/echo`, body, options);
};
export const postEcho = (
  params: Record<string, string> = {},
  body: unknown,
  options: RequestOptions = {},
) => {
  void params;
  return request("POST", `/api/echo`, body, options);
};
export const getMe = (
  params: Record<string, string> = {},
  body: unknown,
  options: RequestOptions = {},
) => {
  void params;
  return request("GET", `/api/me`, body, options);
};
