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

export interface GetVesselSnapshotRequest {
  neLat: number;
  neLon: number;
  swLat: number;
  swLon: number;
  includeCandidates: boolean;
  includeTankers: boolean;
}

export interface GetVesselSnapshotResponse {
  snapshot?: VesselSnapshot;
  fetchedAt: number;
  dataAvailable: boolean;
}

export interface VesselSnapshot {
  snapshotAt: number;
  densityZones: AisDensityZone[];
  disruptions: AisDisruption[];
  sequence: number;
  status?: AisSnapshotStatus;
  candidateReports: SnapshotCandidateReport[];
  tankerReports: SnapshotCandidateReport[];
}

export interface AisDensityZone {
  id: string;
  name: string;
  location?: GeoCoordinates;
  intensity: number;
  deltaPct: number;
  shipsPerDay: number;
  note: string;
}

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

export interface AisDisruption {
  id: string;
  name: string;
  type: AisDisruptionType;
  location?: GeoCoordinates;
  severity: AisDisruptionSeverity;
  changePct: number;
  windowHours: number;
  darkShips: number;
  vesselCount: number;
  region: string;
  description: string;
}

export interface AisSnapshotStatus {
  connected: boolean;
  vessels: number;
  messages: number;
}

export interface SnapshotCandidateReport {
  mmsi: string;
  name: string;
  lat: number;
  lon: number;
  shipType: number;
  heading: number;
  speed: number;
  course: number;
  timestamp: number;
}

export interface ListNavigationalWarningsRequest {
  pageSize: number;
  cursor: string;
  area: string;
}

export interface ListNavigationalWarningsResponse {
  warnings: NavigationalWarning[];
  pagination?: PaginationResponse;
}

export interface NavigationalWarning {
  id: string;
  title: string;
  text: string;
  area: string;
  location?: GeoCoordinates;
  issuedAt: number;
  expiresAt: number;
  authority: string;
}

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

export type AisDisruptionSeverity = "AIS_DISRUPTION_SEVERITY_UNSPECIFIED" | "AIS_DISRUPTION_SEVERITY_LOW" | "AIS_DISRUPTION_SEVERITY_ELEVATED" | "AIS_DISRUPTION_SEVERITY_HIGH";

export type AisDisruptionType = "AIS_DISRUPTION_TYPE_UNSPECIFIED" | "AIS_DISRUPTION_TYPE_GAP_SPIKE" | "AIS_DISRUPTION_TYPE_CHOKEPOINT_CONGESTION";

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

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

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

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

  async getVesselSnapshot(req: GetVesselSnapshotRequest, options?: MaritimeServiceCallOptions): Promise<GetVesselSnapshotResponse> {
    let path = "/api/maritime/v1/get-vessel-snapshot";
    const params = new URLSearchParams();
    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));
    if (req.includeCandidates) params.set("include_candidates", String(req.includeCandidates));
    if (req.includeTankers) params.set("include_tankers", String(req.includeTankers));
    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 GetVesselSnapshotResponse;
  }

  async listNavigationalWarnings(req: ListNavigationalWarningsRequest, options?: MaritimeServiceCallOptions): Promise<ListNavigationalWarningsResponse> {
    let path = "/api/maritime/v1/list-navigational-warnings";
    const params = new URLSearchParams();
    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.area != null && req.area !== "") params.set("area", String(req.area));
    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 ListNavigationalWarningsResponse;
  }

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

