// 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 { GetOrderOutput } from "generated/petstore/api/getOrderOutput";
import type { OrderNotFound } from "generated/petstore/api/orderNotFound";
import type { OrderPriority } from "generated/petstore/api/orderPriority";
import type { OrderStatus } from "generated/petstore/api/orderStatus";
import type { PlaceOrderInput } from "generated/petstore/api/placeOrderInput";
import type { PlaceOrderOutput } from "generated/petstore/api/placeOrderOutput";
import type { ValidationError } from "generated/petstore/api/validationError";

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

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

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

  async getOrder(
    orderId: string,
  ): Promise<GetOrderOutput | OrderNotFound> {
    const headers: Record<string, string> = {};
    const queryParts: string[] = [];
    const queryString = queryParts.join("&");
    const requestUrl = `${this.baseUrl}/orders/${encodeRfc3986(String(orderId))}` + (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["getOrder"]);
    return parsed as GetOrderOutput | OrderNotFound;
  }

  async placeOrder(
    placeOrderInput: PlaceOrderInput,
  ): Promise<PlaceOrderOutput | ValidationError> {
    const headers: Record<string, string> = {};
    headers["Content-Type"] = "application/json";
    const queryParts: string[] = [];
    const queryString = queryParts.join("&");
    const requestUrl = `${this.baseUrl}/orders` + (queryString ? "?" + queryString : "");
    const requestBody = JSON.stringify(dumpApiModel(placeOrderInput));
    const response = await this.fetchFn(
      requestUrl,
      {
        method: "POST",
        headers,
        body: requestBody,
      },
    );
    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["placeOrder"]);
    return parsed as PlaceOrderOutput | ValidationError;
  }
}
