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

export interface ListUnrestEventsRequest {
  start: number;
  end: number;
  pageSize: number;
  cursor: string;
  country: string;
  minSeverity: SeverityLevel;
  neLat: number;
  neLon: number;
  swLat: number;
  swLon: number;
}

export interface ListUnrestEventsResponse {
  events: UnrestEvent[];
  clusters: UnrestCluster[];
  pagination?: PaginationResponse;
}

export interface UnrestEvent {
  id: string;
  title: string;
  summary: string;
  eventType: UnrestEventType;
  city: string;
  country: string;
  region: string;
  location?: GeoCoordinates;
  occurredAt: number;
  severity: SeverityLevel;
  fatalities: number;
  sources: string[];
  sourceType: UnrestSourceType;
  tags: string[];
  actors: string[];
  confidence: ConfidenceLevel;
}

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

export interface UnrestCluster {
  id: string;
  country: string;
  region: string;
  eventCount: number;
  events: UnrestEvent[];
  severity: SeverityLevel;
  startAt: number;
  endAt: number;
  primaryCause: string;
}

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

export type SeverityLevel = "SEVERITY_LEVEL_UNSPECIFIED" | "SEVERITY_LEVEL_LOW" | "SEVERITY_LEVEL_MEDIUM" | "SEVERITY_LEVEL_HIGH";

export type ConfidenceLevel = "CONFIDENCE_LEVEL_UNSPECIFIED" | "CONFIDENCE_LEVEL_LOW" | "CONFIDENCE_LEVEL_MEDIUM" | "CONFIDENCE_LEVEL_HIGH";

export type UnrestEventType = "UNREST_EVENT_TYPE_UNSPECIFIED" | "UNREST_EVENT_TYPE_PROTEST" | "UNREST_EVENT_TYPE_RIOT" | "UNREST_EVENT_TYPE_STRIKE" | "UNREST_EVENT_TYPE_DEMONSTRATION" | "UNREST_EVENT_TYPE_CIVIL_UNREST";

export type UnrestSourceType = "UNREST_SOURCE_TYPE_UNSPECIFIED" | "UNREST_SOURCE_TYPE_ACLED" | "UNREST_SOURCE_TYPE_GDELT" | "UNREST_SOURCE_TYPE_RSS";

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

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

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

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

  async listUnrestEvents(req: ListUnrestEventsRequest, options?: UnrestServiceCallOptions): Promise<ListUnrestEventsResponse> {
    let path = "/api/unrest/v1/list-unrest-events";
    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.country != null && req.country !== "") params.set("country", String(req.country));
    if (req.minSeverity != null && req.minSeverity !== "") params.set("min_severity", String(req.minSeverity));
    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 ListUnrestEventsResponse;
  }

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

