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

import * as z from "zod";
import { remap as remap$ } from "../../lib/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(z.string()).optional(),
  error_description: z.nullable(z.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$,
    });
  });

/** @internal */
export type OAuthErrorResponse$Outbound = {
  error?: string | null | undefined;
  error_description?: string | null | undefined;
};

/** @internal */
export const OAuthErrorResponse$outboundSchema: z.ZodType<
  OAuthErrorResponse$Outbound,
  z.ZodTypeDef,
  OAuthErrorResponse
> = z.instanceof(OAuthErrorResponse)
  .transform(v => v.data$)
  .pipe(
    z.object({
      error: z.nullable(z.string()).optional(),
      errorDescription: z.nullable(z.string()).optional(),
    }).transform((v) => {
      return remap$(v, {
        errorDescription: "error_description",
      });
    }),
  );

/**
 * @internal
 * @deprecated This namespace will be removed in future versions. Use schemas and types that are exported directly from this module.
 */
export namespace OAuthErrorResponse$ {
  /** @deprecated use `OAuthErrorResponse$inboundSchema` instead. */
  export const inboundSchema = OAuthErrorResponse$inboundSchema;
  /** @deprecated use `OAuthErrorResponse$outboundSchema` instead. */
  export const outboundSchema = OAuthErrorResponse$outboundSchema;
  /** @deprecated use `OAuthErrorResponse$Outbound` instead. */
  export type Outbound = OAuthErrorResponse$Outbound;
}
