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

export interface GetConsumerPriceOverviewRequest {
  marketCode: string;
  basketSlug: string;
}

export interface GetConsumerPriceOverviewResponse {
  marketCode: string;
  asOf: string;
  currencyCode: string;
  essentialsIndex: number;
  valueBasketIndex: number;
  wowPct: number;
  momPct: number;
  retailerSpreadPct: number;
  coveragePct: number;
  freshnessLagMin: number;
  topCategories: CategorySnapshot[];
  upstreamUnavailable: boolean;
}

export interface CategorySnapshot {
  slug: string;
  name: string;
  wowPct: number;
  momPct: number;
  currentIndex: number;
  sparkline: number[];
  coveragePct: number;
  itemCount: number;
}

export interface GetConsumerPriceBasketSeriesRequest {
  marketCode: string;
  basketSlug: string;
  range: string;
}

export interface GetConsumerPriceBasketSeriesResponse {
  marketCode: string;
  basketSlug: string;
  asOf: string;
  currencyCode: string;
  range: string;
  essentialsSeries: BasketPoint[];
  valueSeries: BasketPoint[];
  upstreamUnavailable: boolean;
}

export interface BasketPoint {
  date: string;
  index: number;
}

export interface ListConsumerPriceCategoriesRequest {
  marketCode: string;
  basketSlug: string;
  range: string;
}

export interface ListConsumerPriceCategoriesResponse {
  marketCode: string;
  asOf: string;
  range: string;
  categories: CategorySnapshot[];
  upstreamUnavailable: boolean;
}

export interface ListConsumerPriceMoversRequest {
  marketCode: string;
  range: string;
  limit: number;
  categorySlug: string;
}

export interface ListConsumerPriceMoversResponse {
  marketCode: string;
  asOf: string;
  range: string;
  risers: PriceMover[];
  fallers: PriceMover[];
  upstreamUnavailable: boolean;
}

export interface PriceMover {
  productId: string;
  title: string;
  category: string;
  retailerSlug: string;
  changePct: number;
  currentPrice: number;
  currencyCode: string;
}

export interface ListRetailerPriceSpreadsRequest {
  marketCode: string;
  basketSlug: string;
}

export interface ListRetailerPriceSpreadsResponse {
  marketCode: string;
  asOf: string;
  basketSlug: string;
  currencyCode: string;
  retailers: RetailerSpread[];
  spreadPct: number;
  upstreamUnavailable: boolean;
}

export interface RetailerSpread {
  slug: string;
  name: string;
  basketTotal: number;
  deltaVsCheapest: number;
  deltaVsCheapestPct: number;
  itemCount: number;
  freshnessMin: number;
  currencyCode: string;
}

export interface GetConsumerPriceFreshnessRequest {
  marketCode: string;
}

export interface GetConsumerPriceFreshnessResponse {
  marketCode: string;
  asOf: string;
  retailers: RetailerFreshnessInfo[];
  overallFreshnessMin: number;
  stalledCount: number;
  upstreamUnavailable: boolean;
}

export interface RetailerFreshnessInfo {
  slug: string;
  name: string;
  lastRunAt: string;
  status: string;
  parseSuccessRate: number;
  freshnessMin: number;
}

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

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

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

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

  async getConsumerPriceOverview(req: GetConsumerPriceOverviewRequest, options?: ConsumerPricesServiceCallOptions): Promise<GetConsumerPriceOverviewResponse> {
    let path = "/api/consumer-prices/v1/get-consumer-price-overview";
    const params = new URLSearchParams();
    if (req.marketCode != null && req.marketCode !== "") params.set("market_code", String(req.marketCode));
    if (req.basketSlug != null && req.basketSlug !== "") params.set("basket_slug", String(req.basketSlug));
    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 GetConsumerPriceOverviewResponse;
  }

  async getConsumerPriceBasketSeries(req: GetConsumerPriceBasketSeriesRequest, options?: ConsumerPricesServiceCallOptions): Promise<GetConsumerPriceBasketSeriesResponse> {
    let path = "/api/consumer-prices/v1/get-consumer-price-basket-series";
    const params = new URLSearchParams();
    if (req.marketCode != null && req.marketCode !== "") params.set("market_code", String(req.marketCode));
    if (req.basketSlug != null && req.basketSlug !== "") params.set("basket_slug", String(req.basketSlug));
    if (req.range != null && req.range !== "") params.set("range", String(req.range));
    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 GetConsumerPriceBasketSeriesResponse;
  }

  async listConsumerPriceCategories(req: ListConsumerPriceCategoriesRequest, options?: ConsumerPricesServiceCallOptions): Promise<ListConsumerPriceCategoriesResponse> {
    let path = "/api/consumer-prices/v1/list-consumer-price-categories";
    const params = new URLSearchParams();
    if (req.marketCode != null && req.marketCode !== "") params.set("market_code", String(req.marketCode));
    if (req.basketSlug != null && req.basketSlug !== "") params.set("basket_slug", String(req.basketSlug));
    if (req.range != null && req.range !== "") params.set("range", String(req.range));
    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 ListConsumerPriceCategoriesResponse;
  }

  async listConsumerPriceMovers(req: ListConsumerPriceMoversRequest, options?: ConsumerPricesServiceCallOptions): Promise<ListConsumerPriceMoversResponse> {
    let path = "/api/consumer-prices/v1/list-consumer-price-movers";
    const params = new URLSearchParams();
    if (req.marketCode != null && req.marketCode !== "") params.set("market_code", String(req.marketCode));
    if (req.range != null && req.range !== "") params.set("range", String(req.range));
    if (req.limit != null && req.limit !== 0) params.set("limit", String(req.limit));
    if (req.categorySlug != null && req.categorySlug !== "") params.set("category_slug", String(req.categorySlug));
    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 ListConsumerPriceMoversResponse;
  }

  async listRetailerPriceSpreads(req: ListRetailerPriceSpreadsRequest, options?: ConsumerPricesServiceCallOptions): Promise<ListRetailerPriceSpreadsResponse> {
    let path = "/api/consumer-prices/v1/list-retailer-price-spreads";
    const params = new URLSearchParams();
    if (req.marketCode != null && req.marketCode !== "") params.set("market_code", String(req.marketCode));
    if (req.basketSlug != null && req.basketSlug !== "") params.set("basket_slug", String(req.basketSlug));
    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 ListRetailerPriceSpreadsResponse;
  }

  async getConsumerPriceFreshness(req: GetConsumerPriceFreshnessRequest, options?: ConsumerPricesServiceCallOptions): Promise<GetConsumerPriceFreshnessResponse> {
    let path = "/api/consumer-prices/v1/get-consumer-price-freshness";
    const params = new URLSearchParams();
    if (req.marketCode != null && req.marketCode !== "") params.set("market_code", String(req.marketCode));
    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 GetConsumerPriceFreshnessResponse;
  }

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

