/*
 * 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 ConflictCode = {
  Conflict: "conflict",
} as const;
/**
 * A short code indicating the error code returned.
 */
export type ConflictCode = ClosedEnum<typeof ConflictCode>;

export type ConflictError = {
  /**
   * A short code indicating the error code returned.
   */
  code: ConflictCode;
  /**
   * 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;
};

/**
 * This response is sent when a request conflicts with the current state of the server.
 */
export type ConflictData = {
  error: ConflictError;
};

/**
 * This response is sent when a request conflicts with the current state of the server.
 */
export class Conflict extends Error {
  error: ConflictError;

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

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

    this.error = err.error;

    this.name = "Conflict";
  }
}

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

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

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

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

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

/** @internal */
export const ConflictError$outboundSchema: z.ZodType<
  ConflictError$Outbound,
  z.ZodTypeDef,
  ConflictError
> = z.object({
  code: ConflictCode$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 ConflictError$ {
  /** @deprecated use `ConflictError$inboundSchema` instead. */
  export const inboundSchema = ConflictError$inboundSchema;
  /** @deprecated use `ConflictError$outboundSchema` instead. */
  export const outboundSchema = ConflictError$outboundSchema;
  /** @deprecated use `ConflictError$Outbound` instead. */
  export type Outbound = ConflictError$Outbound;
}

export function conflictErrorToJSON(conflictError: ConflictError): string {
  return JSON.stringify(ConflictError$outboundSchema.parse(conflictError));
}

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

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

/** @internal */
export type Conflict$Outbound = {
  error: ConflictError$Outbound;
};

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

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