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

export interface SearchImageryRequest {
  bbox: string;
  datetime: string;
  source: string;
  limit: number;
}

export interface SearchImageryResponse {
  scenes: ImageryScene[];
  totalResults: number;
  cacheHit: boolean;
}

export interface ImageryScene {
  id: string;
  satellite: string;
  datetime: string;
  resolutionM: number;
  mode: string;
  geometryGeojson: string;
  previewUrl: string;
  assetUrl: 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 ImageryServiceClientOptions {
  fetch?: typeof fetch;
  defaultHeaders?: Record<string, string>;
}

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

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

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

  async searchImagery(req: SearchImageryRequest, options?: ImageryServiceCallOptions): Promise<SearchImageryResponse> {
    let path = "/api/imagery/v1/search-imagery";
    const params = new URLSearchParams();
    if (req.bbox != null && req.bbox !== "") params.set("bbox", String(req.bbox));
    if (req.datetime != null && req.datetime !== "") params.set("datetime", String(req.datetime));
    if (req.source != null && req.source !== "") params.set("source", String(req.source));
    if (req.limit != null && req.limit !== 0) params.set("limit", String(req.limit));
    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 SearchImageryResponse;
  }

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

