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

import * as z from "zod/v3";
import { remap as remap$ } from "../../lib/primitives.js";
import * as types from "../../types/primitives.js";
import * as components from "../components/index.js";
import { IamClientError } from "./iamclienterror.js";

/**
 * Error response conforming to RFC 9457 (Problem Details for HTTP APIs).
 *
 * @remarks
 * See: https://www.rfc-editor.org/rfc/rfc9457.html
 */
export type ErrDetailsData = {
  /**
   * A URI reference that identifies the problem type. When dereferenced,
   *
   * @remarks
   * it should provide human-readable documentation for the problem type.
   * Use "about:blank" when no specific type documentation exists.
   */
  type: string;
  /**
   * A short, human-readable summary of the problem type. Should not change between occurrences.
   */
  title: string;
  /**
   * The HTTP status code generated by the origin server for this occurrence of the problem.
   */
  status: number;
  /**
   * A human-readable explanation specific to this occurrence of the problem.
   */
  detail?: string | undefined;
  /**
   * A URI reference that identifies the specific occurrence of the problem (typically the request path).
   */
  instance?: string | undefined;
  /**
   * An array of detailed validation error objects when multiple issues exist.
   *
   * @remarks
   * Each entry identifies a specific field-level or parameter-level error.
   */
  errors?: Array<components.ValidationError> | undefined;
  /**
   * Unique identifier for the request, useful for tracing and debugging with support.
   */
  requestId?: string | undefined;
  /**
   * The timestamp when the error occurred.
   */
  timestamp?: Date | undefined;
};

/**
 * Error response conforming to RFC 9457 (Problem Details for HTTP APIs).
 *
 * @remarks
 * See: https://www.rfc-editor.org/rfc/rfc9457.html
 */
export class ErrDetails extends IamClientError {
  /**
   * A URI reference that identifies the problem type. When dereferenced,
   *
   * @remarks
   * it should provide human-readable documentation for the problem type.
   * Use "about:blank" when no specific type documentation exists.
   */
  type: string;
  /**
   * A short, human-readable summary of the problem type. Should not change between occurrences.
   */
  title: string;
  /**
   * The HTTP status code generated by the origin server for this occurrence of the problem.
   */
  status: number;
  /**
   * A human-readable explanation specific to this occurrence of the problem.
   */
  detail?: string | undefined;
  /**
   * A URI reference that identifies the specific occurrence of the problem (typically the request path).
   */
  instance?: string | undefined;
  /**
   * An array of detailed validation error objects when multiple issues exist.
   *
   * @remarks
   * Each entry identifies a specific field-level or parameter-level error.
   */
  errors?: Array<components.ValidationError> | undefined;
  /**
   * Unique identifier for the request, useful for tracing and debugging with support.
   */
  requestId?: string | undefined;
  /**
   * The timestamp when the error occurred.
   */
  timestamp?: Date | undefined;

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

  constructor(
    err: ErrDetailsData,
    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;
    this.type = err.type;
    this.title = err.title;
    this.status = err.status;
    if (err.detail != null) this.detail = err.detail;
    if (err.instance != null) this.instance = err.instance;
    if (err.errors != null) this.errors = err.errors;
    if (err.requestId != null) this.requestId = err.requestId;
    if (err.timestamp != null) this.timestamp = err.timestamp;

    this.name = "ErrDetails";
  }
}

/** @internal */
export const ErrDetails$inboundSchema: z.ZodType<
  ErrDetails,
  z.ZodTypeDef,
  unknown
> = z.object({
  type: types.string(),
  title: types.string(),
  status: types.number(),
  detail: types.optional(types.string()),
  instance: types.optional(types.string()),
  errors: types.optional(z.array(components.ValidationError$inboundSchema)),
  request_id: types.optional(types.string()),
  timestamp: types.optional(types.date()),
  request$: z.instanceof(Request),
  response$: z.instanceof(Response),
  body$: z.string(),
})
  .transform((v) => {
    const remapped = remap$(v, {
      "request_id": "requestId",
    });

    return new ErrDetails(remapped, {
      request: v.request$,
      response: v.response$,
      body: v.body$,
    });
  });
