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

import * as z from "zod";
import { remap as remap$ } from "../../lib/primitives.js";
import { safeParse } from "../../lib/schemas.js";
import { ClosedEnum } from "../../types/enums.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "./sdkvalidationerror.js";

/**
 * A short code indicating the error code returned.
 */
export const RateLimitExceededCode = {
  RateLimitExceeded: "rate_limit_exceeded",
} as const;
/**
 * A short code indicating the error code returned.
 */
export type RateLimitExceededCode = ClosedEnum<typeof RateLimitExceededCode>;

export type RateLimitExceededError = {
  /**
   * A short code indicating the error code returned.
   */
  code: RateLimitExceededCode;
  /**
   * A human readable explanation of what went wrong.
   */
  message: string;
  /**
   * A link to our documentation with more details about this error code
   */
  docUrl?: string | undefined;
};

/**
 * The user has sent too many requests in a given amount of time ("rate limiting")
 */
export type RateLimitExceededData = {
  error: RateLimitExceededError;
};

/**
 * The user has sent too many requests in a given amount of time ("rate limiting")
 */
export class RateLimitExceeded extends Error {
  error: RateLimitExceededError;

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

  constructor(err: RateLimitExceededData) {
    const message = err.error?.message || "API error occurred";
    super(message);
    this.data$ = err;

    this.error = err.error;

    this.name = "RateLimitExceeded";
  }
}

/** @internal */
export const RateLimitExceededCode$inboundSchema: z.ZodNativeEnum<
  typeof RateLimitExceededCode
> = z.nativeEnum(RateLimitExceededCode);

/** @internal */
export const RateLimitExceededCode$outboundSchema: z.ZodNativeEnum<
  typeof RateLimitExceededCode
> = RateLimitExceededCode$inboundSchema;

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

/** @internal */
export const RateLimitExceededError$inboundSchema: z.ZodType<
  RateLimitExceededError,
  z.ZodTypeDef,
  unknown
> = z.object({
  code: RateLimitExceededCode$inboundSchema,
  message: z.string(),
  doc_url: z.string().optional(),
}).transform((v) => {
  return remap$(v, {
    "doc_url": "docUrl",
  });
});

/** @internal */
export type RateLimitExceededError$Outbound = {
  code: string;
  message: string;
  doc_url?: string | undefined;
};

/** @internal */
export const RateLimitExceededError$outboundSchema: z.ZodType<
  RateLimitExceededError$Outbound,
  z.ZodTypeDef,
  RateLimitExceededError
> = z.object({
  code: RateLimitExceededCode$outboundSchema,
  message: z.string(),
  docUrl: z.string().optional(),
}).transform((v) => {
  return remap$(v, {
    docUrl: "doc_url",
  });
});

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

export function rateLimitExceededErrorToJSON(
  rateLimitExceededError: RateLimitExceededError,
): string {
  return JSON.stringify(
    RateLimitExceededError$outboundSchema.parse(rateLimitExceededError),
  );
}

export function rateLimitExceededErrorFromJSON(
  jsonString: string,
): SafeParseResult<RateLimitExceededError, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => RateLimitExceededError$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'RateLimitExceededError' from JSON`,
  );
}

/** @internal */
export const RateLimitExceeded$inboundSchema: z.ZodType<
  RateLimitExceeded,
  z.ZodTypeDef,
  unknown
> = z.object({
  error: z.lazy(() => RateLimitExceededError$inboundSchema),
})
  .transform((v) => {
    return new RateLimitExceeded(v);
  });

/** @internal */
export type RateLimitExceeded$Outbound = {
  error: RateLimitExceededError$Outbound;
};

/** @internal */
export const RateLimitExceeded$outboundSchema: z.ZodType<
  RateLimitExceeded$Outbound,
  z.ZodTypeDef,
  RateLimitExceeded
> = z.instanceof(RateLimitExceeded)
  .transform(v => v.data$)
  .pipe(z.object({
    error: z.lazy(() => RateLimitExceededError$outboundSchema),
  }));

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