// Code generated by protoc-gen-ts-client. DO NOT EDIT.
// source: worldmonitor/positive_events/v1/service.proto

export interface ListPositiveGeoEventsRequest {
}

export interface ListPositiveGeoEventsResponse {
  events: PositiveGeoEvent[];
  fetchedAt: number;
  stale: boolean;
}

export interface PositiveGeoEvent {
  latitude: number;
  longitude: number;
  name: string;
  category: string;
  count: number;
  timestamp: number;
}

export interface FieldViolation {
  field: string;
  description: string;
}

export class ValidationError extends Error {
  violations: FieldViolation[];

  constructor(violations: FieldViolation[]) {
    super("Validation failed");
    this.name = "ValidationError";
    this.violations = violations;
  }
}

export class ApiError extends Error {
  statusCode: number;
  body: string;

  constructor(statusCode: number, message: string, body: string) {
    super(message);
    this.name = "ApiError";
    this.statusCode = statusCode;
    this.body = body;
  }
}

export interface PositiveEventsServiceClientOptions {
  fetch?: typeof fetch;
  defaultHeaders?: Record<string, string>;
}

export interface PositiveEventsServiceCallOptions {
  headers?: Record<string, string>;
  signal?: AbortSignal;
}

export class PositiveEventsServiceClient {
  private baseURL: string;
  private fetchFn: typeof fetch;
  private defaultHeaders: Record<string, string>;

  constructor(baseURL: string, options?: PositiveEventsServiceClientOptions) {
    this.baseURL = baseURL.replace(/\/+$/, "");
    this.fetchFn = options?.fetch ?? globalThis.fetch;
    this.defaultHeaders = { ...options?.defaultHeaders };
  }

  async listPositiveGeoEvents(_req: ListPositiveGeoEventsRequest, options?: PositiveEventsServiceCallOptions): Promise<ListPositiveGeoEventsResponse> {
    let path = "/api/positive-events/v1/list-positive-geo-events";
    const url = this.baseURL + path;

    const headers: Record<string, string> = {
      "Content-Type": "application/json",
      ...this.defaultHeaders,
      ...options?.headers,
    };

    const resp = await this.fetchFn(url, {
      method: "GET",
      headers,
      signal: options?.signal,
    });

    if (!resp.ok) {
      return this.handleError(resp);
    }

    return await resp.json() as ListPositiveGeoEventsResponse;
  }

  private async handleError(resp: Response): Promise<never> {
    const body = await resp.text();
    if (resp.status === 400) {
      try {
        const parsed = JSON.parse(body);
        if (parsed.violations) {
          throw new ValidationError(parsed.violations);
        }
      } catch (e) {
        if (e instanceof ValidationError) throw e;
      }
    }
    throw new ApiError(resp.status, `Request failed with status ${resp.status}`, body);
  }
}

