/*
 * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
 */

import * as z from "zod/v4-mini";
import {
  ValidationError,
  ValidationError$inboundSchema,
  ValidationError$Outbound,
  ValidationError$outboundSchema,
} from "../components/validationerror.js";
import { PolarError } from "./polarerror.js";

export type HTTPValidationErrorData = {
  detail?: Array<ValidationError> | undefined;
};

export class HTTPValidationError extends PolarError {
  detail?: Array<ValidationError> | undefined;

  /** The original data that was passed to this error instance. */
  data$: HTTPValidationErrorData;

  constructor(
    err: HTTPValidationErrorData,
    httpMeta: { response: Response; request: Request; body: string },
  ) {
    const message = "message" in err && typeof err.message === "string"
      ? err.message
      : `API error occurred: ${JSON.stringify(err)}`;
    super(message, httpMeta);
    this.data$ = err;
    if (err.detail != null) this.detail = err.detail;

    this.name = "HTTPValidationError";
  }
}

/** @internal */
export const HTTPValidationError$inboundSchema: z.ZodMiniType<
  HTTPValidationError,
  unknown
> = z.pipe(
  z.object({
    detail: z.optional(z.array(ValidationError$inboundSchema)),
    request$: z.custom<Request>(x => x instanceof Request),
    response$: z.custom<Response>(x => x instanceof Response),
    body$: z.string(),
  }),
  z.transform((v) => {
    return new HTTPValidationError(v, {
      request: v.request$,
      response: v.response$,
      body: v.body$,
    });
  }),
);

/** @internal */
export type HTTPValidationError$Outbound = {
  detail?: Array<ValidationError$Outbound> | undefined;
};

/** @internal */
export const HTTPValidationError$outboundSchema: z.ZodMiniType<
  HTTPValidationError$Outbound,
  HTTPValidationError
> = z.pipe(
  z.pipe(
    z.custom<HTTPValidationError>(x => x instanceof HTTPValidationError),
    z.transform(v => v.data$),
  ),
  z.object({
    detail: z.optional(z.array(ValidationError$outboundSchema)),
  }),
);
