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

export interface ListThermalEscalationsRequest {
  maxItems: number;
}

export interface ListThermalEscalationsResponse {
  fetchedAt: string;
  observationWindowHours: number;
  sourceVersion: string;
  clusters: ThermalEscalationCluster[];
  summary?: ThermalEscalationSummary;
  dataAvailable: boolean;
}

export interface ThermalEscalationCluster {
  id: string;
  centroid?: GeoCoordinates;
  countryCode: string;
  countryName: string;
  regionLabel: string;
  firstDetectedAt: string;
  lastDetectedAt: string;
  observationCount: number;
  uniqueSourceCount: number;
  maxBrightness: number;
  avgBrightness: number;
  maxFrp: number;
  totalFrp: number;
  nightDetectionShare: number;
  baselineExpectedCount: number;
  baselineExpectedFrp: number;
  countDelta: number;
  frpDelta: number;
  zScore: number;
  persistenceHours: number;
  status: ThermalStatus;
  context: ThermalContext;
  confidence: ThermalConfidence;
  strategicRelevance: ThermalStrategicRelevance;
  nearbyAssets: string[];
  narrativeFlags: string[];
}

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

export interface ThermalEscalationSummary {
  clusterCount: number;
  elevatedCount: number;
  spikeCount: number;
  persistentCount: number;
  conflictAdjacentCount: number;
  highRelevanceCount: number;
}

export type ThermalConfidence = "THERMAL_CONFIDENCE_UNSPECIFIED" | "THERMAL_CONFIDENCE_LOW" | "THERMAL_CONFIDENCE_MEDIUM" | "THERMAL_CONFIDENCE_HIGH";

export type ThermalContext = "THERMAL_CONTEXT_UNSPECIFIED" | "THERMAL_CONTEXT_WILDLAND" | "THERMAL_CONTEXT_URBAN_EDGE" | "THERMAL_CONTEXT_INDUSTRIAL" | "THERMAL_CONTEXT_ENERGY_ADJACENT" | "THERMAL_CONTEXT_CONFLICT_ADJACENT" | "THERMAL_CONTEXT_LOGISTICS_ADJACENT" | "THERMAL_CONTEXT_MIXED";

export type ThermalStatus = "THERMAL_STATUS_UNSPECIFIED" | "THERMAL_STATUS_NORMAL" | "THERMAL_STATUS_ELEVATED" | "THERMAL_STATUS_SPIKE" | "THERMAL_STATUS_PERSISTENT";

export type ThermalStrategicRelevance = "THERMAL_RELEVANCE_UNSPECIFIED" | "THERMAL_RELEVANCE_LOW" | "THERMAL_RELEVANCE_MEDIUM" | "THERMAL_RELEVANCE_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 ThermalServiceClientOptions {
  fetch?: typeof fetch;
  defaultHeaders?: Record<string, string>;
}

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

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

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

  async listThermalEscalations(req: ListThermalEscalationsRequest, options?: ThermalServiceCallOptions): Promise<ListThermalEscalationsResponse> {
    let path = "/api/thermal/v1/list-thermal-escalations";
    const params = new URLSearchParams();
    if (req.maxItems != null && req.maxItems !== 0) params.set("max_items", String(req.maxItems));
    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 ListThermalEscalationsResponse;
  }

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

