// Generated by smithplates HTTP codegen. Do not edit.

import { dumpApiModel } from "generated/petstore/api/petstore/modelValidation";
import { parseClientResponse } from "generated/petstore/api/petstore/client/clientResponse";
import { OPERATION_HTTP_BINDINGS } from "generated/petstore/api/petstore/client/operationBindings";
import type { CategoryNotFound } from "generated/petstore/api/categoryNotFound";
import type { GetCategoryOutput } from "generated/petstore/api/getCategoryOutput";

function encodeRfc3986(value: string): string {
  return encodeURIComponent(value).replace(
    /[!'()*]/g,
    (character) => "%" + character.charCodeAt(0).toString(16).toUpperCase(),
  );
}

export class CategoriesApiClient {
  private readonly baseUrl: string;
  private readonly fetchFn: typeof fetch;

  constructor(baseUrl: string, fetchFn: typeof fetch = fetch) {
    this.baseUrl = baseUrl.replace(/\/+$/, "");
    this.fetchFn = fetchFn;
  }

  async getCategory(
    categoryId: string,
  ): Promise<GetCategoryOutput | CategoryNotFound> {
    const headers: Record<string, string> = {};
    const queryParts: string[] = [];
    const queryString = queryParts.join("&");
    const requestUrl = `${this.baseUrl}/categories/${encodeRfc3986(String(categoryId))}` + (queryString ? "?" + queryString : "");
    const response = await this.fetchFn(
      requestUrl,
      {
        method: "GET",
        headers,
      },
    );
    const normalized = {
      status: response.status,
      headers: Object.fromEntries(response.headers.entries()),
      text: () => response.text(),
      json: async () => await response.json(),
    };
    const parsed = await parseClientResponse(normalized, OPERATION_HTTP_BINDINGS["getCategory"]);
    return parsed as GetCategoryOutput | CategoryNotFound;
  }
}
