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

export interface ListWebcamsRequest {
  zoom: number;
  boundW: number;
  boundS: number;
  boundE: number;
  boundN: number;
}

export interface ListWebcamsResponse {
  webcams: WebcamEntry[];
  clusters: WebcamCluster[];
  totalInView: number;
}

export interface WebcamEntry {
  webcamId: string;
  title: string;
  lat: number;
  lng: number;
  category: string;
  country: string;
}

export interface WebcamCluster {
  lat: number;
  lng: number;
  count: number;
  categories: string[];
}

export interface GetWebcamImageRequest {
  webcamId: string;
}

export interface GetWebcamImageResponse {
  thumbnailUrl: string;
  playerUrl: string;
  title: string;
  windyUrl: string;
  lastUpdated: string;
  error: string;
}

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

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

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

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

  async listWebcams(req: ListWebcamsRequest, options?: WebcamServiceCallOptions): Promise<ListWebcamsResponse> {
    let path = "/api/webcam/v1/list-webcams";
    const params = new URLSearchParams();
    if (req.zoom != null && req.zoom !== 0) params.set("zoom", String(req.zoom));
    if (req.boundW != null && req.boundW !== 0) params.set("bound_w", String(req.boundW));
    if (req.boundS != null && req.boundS !== 0) params.set("bound_s", String(req.boundS));
    if (req.boundE != null && req.boundE !== 0) params.set("bound_e", String(req.boundE));
    if (req.boundN != null && req.boundN !== 0) params.set("bound_n", String(req.boundN));
    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 ListWebcamsResponse;
  }

  async getWebcamImage(req: GetWebcamImageRequest, options?: WebcamServiceCallOptions): Promise<GetWebcamImageResponse> {
    let path = "/api/webcam/v1/get-webcam-image";
    const params = new URLSearchParams();
    if (req.webcamId != null && req.webcamId !== "") params.set("webcam_id", String(req.webcamId));
    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 GetWebcamImageResponse;
  }

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

