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

import { dumpApiModel } from "generated/example/queryAuthApi/modelValidation";
import { parseClientResponse } from "generated/example/queryAuthApi/client/clientResponse";
import { OPERATION_HTTP_BINDINGS } from "generated/example/queryAuthApi/client/operationBindings";
import type { AuthProvider } from "generated/example/queryAuthApi/client/operationBindings";
import { OPERATION_AUTH_BINDINGS, applyOperationAuth } from "generated/example/queryAuthApi/client/operationBindings";
import type { AuthOutput } from "generated/example/authOutput";

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

export class QueryAuthApiClient {
  private readonly baseUrl: string;
  private readonly fetchFn: typeof fetch;
  private readonly authProvider: AuthProvider | undefined;

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

  async queryApiKey(
    filter: string | null,
  ): Promise<AuthOutput> {
    const headers: Record<string, string> = {};
    const queryParts: string[] = [];
    if (filter !== null && filter !== undefined) {
      queryParts.push(encodeRfc3986("filter") + "=" + encodeRfc3986(String(filter)));
    }
    applyOperationAuth(
      OPERATION_AUTH_BINDINGS["queryApiKey"],
      this.authProvider,
      headers,
      queryParts,
    );
    const queryString = queryParts.join("&");
    const requestUrl = `${this.baseUrl}/query-key` + (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["queryApiKey"]);
    return parsed as AuthOutput;
  }
}
