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

export interface ListFireDetectionsRequest {
  start: number;
  end: number;
  pageSize: number;
  cursor: string;
  neLat: number;
  neLon: number;
  swLat: number;
  swLon: number;
}

export interface ListFireDetectionsResponse {
  fireDetections: FireDetection[];
  pagination?: PaginationResponse;
  fetchedAt: number;
  dataAvailable: boolean;
}

export interface FireDetection {
  id: string;
  location?: GeoCoordinates;
  brightness: number;
  frp: number;
  confidence: FireConfidence;
  satellite: string;
  detectedAt: number;
  region: string;
  dayNight: string;
  possibleExplosion: boolean;
}

export interface GeoCoordinates {
  latitude: number;
  longitude: number;
}

export interface PaginationResponse {
  nextCursor: string;
  totalCount: number;
}

export type FireConfidence = "FIRE_CONFIDENCE_UNSPECIFIED" | "FIRE_CONFIDENCE_LOW" | "FIRE_CONFIDENCE_NOMINAL" | "FIRE_CONFIDENCE_HIGH";

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

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

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

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

  async listFireDetections(req: ListFireDetectionsRequest, options?: WildfireServiceCallOptions): Promise<ListFireDetectionsResponse> {
    let path = "/api/wildfire/v1/list-fire-detections";
    const params = new URLSearchParams();
    if (req.start != null && req.start !== 0) params.set("start", String(req.start));
    if (req.end != null && req.end !== 0) params.set("end", String(req.end));
    if (req.pageSize != null && req.pageSize !== 0) params.set("page_size", String(req.pageSize));
    if (req.cursor != null && req.cursor !== "") params.set("cursor", String(req.cursor));
    if (req.neLat != null && req.neLat !== 0) params.set("ne_lat", String(req.neLat));
    if (req.neLon != null && req.neLon !== 0) params.set("ne_lon", String(req.neLon));
    if (req.swLat != null && req.swLat !== 0) params.set("sw_lat", String(req.swLat));
    if (req.swLon != null && req.swLon !== 0) params.set("sw_lon", String(req.swLon));
    const url = this.baseURL + path + (params.toString() ? "?" + params.toString() : "");

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

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

