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

export interface ListRadiationObservationsRequest {
  maxItems: number;
}

export interface ListRadiationObservationsResponse {
  observations: RadiationObservation[];
  fetchedAt: number;
  epaCount: number;
  safecastCount: number;
  anomalyCount: number;
  elevatedCount: number;
  spikeCount: number;
  corroboratedCount: number;
  lowConfidenceCount: number;
  conflictingCount: number;
  convertedFromCpmCount: number;
}

export interface RadiationObservation {
  id: string;
  source: RadiationSource;
  locationName: string;
  country: string;
  location?: GeoCoordinates;
  value: number;
  unit: string;
  observedAt: number;
  freshness: RadiationFreshness;
  baselineValue: number;
  delta: number;
  zScore: number;
  severity: RadiationSeverity;
  contributingSources: RadiationSource[];
  confidence: RadiationConfidence;
  corroborated: boolean;
  conflictingSources: boolean;
  convertedFromCpm: boolean;
  sourceCount: number;
}

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

export type RadiationConfidence = "RADIATION_CONFIDENCE_UNSPECIFIED" | "RADIATION_CONFIDENCE_LOW" | "RADIATION_CONFIDENCE_MEDIUM" | "RADIATION_CONFIDENCE_HIGH";

export type RadiationFreshness = "RADIATION_FRESHNESS_UNSPECIFIED" | "RADIATION_FRESHNESS_LIVE" | "RADIATION_FRESHNESS_RECENT" | "RADIATION_FRESHNESS_HISTORICAL";

export type RadiationSeverity = "RADIATION_SEVERITY_UNSPECIFIED" | "RADIATION_SEVERITY_NORMAL" | "RADIATION_SEVERITY_ELEVATED" | "RADIATION_SEVERITY_SPIKE";

export type RadiationSource = "RADIATION_SOURCE_UNSPECIFIED" | "RADIATION_SOURCE_EPA_RADNET" | "RADIATION_SOURCE_SAFECAST";

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

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

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

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

  async listRadiationObservations(req: ListRadiationObservationsRequest, options?: RadiationServiceCallOptions): Promise<ListRadiationObservationsResponse> {
    let path = "/api/radiation/v1/list-radiation-observations";
    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 ListRadiationObservationsResponse;
  }

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

