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

export interface ListDiseaseOutbreaksRequest {
}

export interface ListDiseaseOutbreaksResponse {
  outbreaks: DiseaseOutbreakItem[];
  fetchedAt: number;
  alertLevelMethodologyVersion: string;
}

export interface DiseaseOutbreakItem {
  id: string;
  disease: string;
  location: string;
  countryCode: string;
  alertLevel: string;
  summary: string;
  sourceUrl: string;
  publishedAt: number;
  sourceName: string;
  lat: number;
  lng: number;
  cases: number;
}

export interface ListAirQualityAlertsRequest {
}

export interface ListAirQualityAlertsResponse {
  alerts: AirQualityAlert[];
  fetchedAt: number;
}

export interface AirQualityAlert {
  city: string;
  countryCode: string;
  lat: number;
  lng: number;
  pm25: number;
  aqi: number;
  riskLevel: string;
  pollutant: string;
  measuredAt: number;
  source: string;
}

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 HealthServiceClientOptions {
  fetch?: typeof fetch;
  defaultHeaders?: Record<string, string>;
}

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

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

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

  async listDiseaseOutbreaks(_req: ListDiseaseOutbreaksRequest, options?: HealthServiceCallOptions): Promise<ListDiseaseOutbreaksResponse> {
    let path = "/api/health/v1/list-disease-outbreaks";
    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 ListDiseaseOutbreaksResponse;
  }

  async listAirQualityAlerts(_req: ListAirQualityAlertsRequest, options?: HealthServiceCallOptions): Promise<ListAirQualityAlertsResponse> {
    let path = "/api/health/v1/list-air-quality-alerts";
    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 ListAirQualityAlertsResponse;
  }

  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);
  }
}

