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

export interface ListArxivPapersRequest {
  pageSize: number;
  cursor: string;
  category: string;
  query: string;
}

export interface ListArxivPapersResponse {
  papers: ArxivPaper[];
  pagination?: PaginationResponse;
}

export interface ArxivPaper {
  id: string;
  title: string;
  summary: string;
  authors: string[];
  categories: string[];
  publishedAt: number;
  url: string;
}

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

export interface ListTrendingReposRequest {
  pageSize: number;
  cursor: string;
  language: string;
  period: string;
}

export interface ListTrendingReposResponse {
  repos: GithubRepo[];
  pagination?: PaginationResponse;
}

export interface GithubRepo {
  fullName: string;
  description: string;
  language: string;
  stars: number;
  starsToday: number;
  forks: number;
  url: string;
}

export interface ListHackernewsItemsRequest {
  pageSize: number;
  cursor: string;
  feedType: string;
}

export interface ListHackernewsItemsResponse {
  items: HackernewsItem[];
  pagination?: PaginationResponse;
}

export interface HackernewsItem {
  id: number;
  title: string;
  url: string;
  score: number;
  commentCount: number;
  by: string;
  submittedAt: number;
}

export interface ListTechEventsRequest {
  type: string;
  mappable: boolean;
  limit: number;
  days: number;
}

export interface ListTechEventsResponse {
  success: boolean;
  count: number;
  conferenceCount: number;
  mappableCount: number;
  lastUpdated: string;
  events: TechEvent[];
  error: string;
}

export interface TechEvent {
  id: string;
  title: string;
  type: string;
  location: string;
  coords?: TechEventCoords;
  startDate: string;
  endDate: string;
  url: string;
  source: string;
  description: string;
}

export interface TechEventCoords {
  lat: number;
  lng: number;
  country: string;
  original: string;
  virtual: boolean;
}

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

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

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

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

  async listArxivPapers(req: ListArxivPapersRequest, options?: ResearchServiceCallOptions): Promise<ListArxivPapersResponse> {
    let path = "/api/research/v1/list-arxiv-papers";
    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.category != null && req.category !== "") params.set("category", String(req.category));
    if (req.query != null && req.query !== "") params.set("query", String(req.query));
    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 ListArxivPapersResponse;
  }

  async listTrendingRepos(req: ListTrendingReposRequest, options?: ResearchServiceCallOptions): Promise<ListTrendingReposResponse> {
    let path = "/api/research/v1/list-trending-repos";
    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.language != null && req.language !== "") params.set("language", String(req.language));
    if (req.period != null && req.period !== "") params.set("period", String(req.period));
    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 ListTrendingReposResponse;
  }

  async listHackernewsItems(req: ListHackernewsItemsRequest, options?: ResearchServiceCallOptions): Promise<ListHackernewsItemsResponse> {
    let path = "/api/research/v1/list-hackernews-items";
    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.feedType != null && req.feedType !== "") params.set("feed_type", String(req.feedType));
    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 ListHackernewsItemsResponse;
  }

  async listTechEvents(req: ListTechEventsRequest, options?: ResearchServiceCallOptions): Promise<ListTechEventsResponse> {
    let path = "/api/research/v1/list-tech-events";
    const params = new URLSearchParams();
    if (req.type != null && req.type !== "") params.set("type", String(req.type));
    if (req.mappable) params.set("mappable", String(req.mappable));
    if (req.limit != null && req.limit !== 0) params.set("limit", String(req.limit));
    if (req.days != null && req.days !== 0) params.set("days", String(req.days));
    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 ListTechEventsResponse;
  }

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

