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

export interface ListSanctionsPressureRequest {
  maxItems: number;
}

export interface ListSanctionsPressureResponse {
  entries: SanctionsEntry[];
  countries: CountrySanctionsPressure[];
  programs: ProgramSanctionsPressure[];
  fetchedAt: string;
  datasetDate: string;
  totalCount: number;
  sdnCount: number;
  consolidatedCount: number;
  newEntryCount: number;
  vesselCount: number;
  aircraftCount: number;
}

export interface SanctionsEntry {
  id: string;
  name: string;
  entityType: SanctionsEntityType;
  countryCodes: string[];
  countryNames: string[];
  programs: string[];
  sourceLists: string[];
  effectiveAt: string;
  isNew: boolean;
  note: string;
}

export interface CountrySanctionsPressure {
  countryCode: string;
  countryName: string;
  entryCount: number;
  newEntryCount: number;
  vesselCount: number;
  aircraftCount: number;
}

export interface ProgramSanctionsPressure {
  program: string;
  entryCount: number;
  newEntryCount: number;
}

export interface LookupSanctionEntityRequest {
  q: string;
  maxResults: number;
}

export interface LookupSanctionEntityResponse {
  results: SanctionEntityMatch[];
  total: number;
  source: string;
}

export interface SanctionEntityMatch {
  id: string;
  name: string;
  entityType: string;
  countryCodes: string[];
  programs: string[];
}

export type SanctionsEntityType = "SANCTIONS_ENTITY_TYPE_UNSPECIFIED" | "SANCTIONS_ENTITY_TYPE_ENTITY" | "SANCTIONS_ENTITY_TYPE_INDIVIDUAL" | "SANCTIONS_ENTITY_TYPE_VESSEL" | "SANCTIONS_ENTITY_TYPE_AIRCRAFT";

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

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

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

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

  async listSanctionsPressure(req: ListSanctionsPressureRequest, options?: SanctionsServiceCallOptions): Promise<ListSanctionsPressureResponse> {
    let path = "/api/sanctions/v1/list-sanctions-pressure";
    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 ListSanctionsPressureResponse;
  }

  async lookupSanctionEntity(req: LookupSanctionEntityRequest, options?: SanctionsServiceCallOptions): Promise<LookupSanctionEntityResponse> {
    let path = "/api/sanctions/v1/lookup-sanction-entity";
    const params = new URLSearchParams();
    if (req.q != null && req.q !== "") params.set("q", String(req.q));
    if (req.maxResults != null && req.maxResults !== 0) params.set("max_results", String(req.maxResults));
    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 LookupSanctionEntityResponse;
  }

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

