import type { ApiResponse } from "@pmap/shared-types";

export class ApiClient {
  constructor(private readonly baseUrl: string) {}

  async get<T>(path: string): Promise<ApiResponse<T>> {
    const response = await fetch(`${this.baseUrl}${path}`, { credentials: "include" });
    return response.json() as Promise<ApiResponse<T>>;
  }

  async post<T>(path: string, body: unknown, idempotencyKey?: string): Promise<ApiResponse<T>> {
    const response = await fetch(`${this.baseUrl}${path}`, {
      method: "POST",
      credentials: "include",
      headers: {
        "content-type": "application/json",
        ...(idempotencyKey ? { "idempotency-key": idempotencyKey } : {})
      },
      body: JSON.stringify(body)
    });
    return response.json() as Promise<ApiResponse<T>>;
  }
}
