/*
 * 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 { IamClientError } from "./iamclienterror.js";

export type OAuthErrorResponseData = {
  error?: string | null | undefined;
  errorDescription?: string | null | undefined;
};

export class OAuthErrorResponse extends IamClientError {
  error?: string | null | undefined;
  errorDescription?: string | null | undefined;

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

  constructor(
    err: OAuthErrorResponseData,
    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.error != null) this.error = err.error;
    if (err.errorDescription != null) {
      this.errorDescription = err.errorDescription;
    }

    this.name = "OAuthErrorResponse";
  }
}

/** @internal */
export const OAuthErrorResponse$inboundSchema: z.ZodType<
  OAuthErrorResponse,
  z.ZodTypeDef,
  unknown
> = z.object({
  error: z.nullable(types.string()).optional(),
  error_description: z.nullable(types.string()).optional(),
  request$: z.instanceof(Request),
  response$: z.instanceof(Response),
  body$: z.string(),
})
  .transform((v) => {
    const remapped = remap$(v, {
      "error_description": "errorDescription",
    });

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