/*
 * 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 { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "./sdkvalidationerror.js";

export type GetCountriesLibraryLibraryErrors = {
  code?: number | undefined;
  message?: string | undefined;
  status?: number | undefined;
};

/**
 * Unauthorized - Returned if the X-Plex-Token is missing from the header or query.
 */
export type GetCountriesLibraryUnauthorizedData = {
  errors?: Array<GetCountriesLibraryLibraryErrors> | undefined;
  /**
   * Raw HTTP response; suitable for custom response parsing
   */
  rawResponse?: Response | undefined;
};

/**
 * Unauthorized - Returned if the X-Plex-Token is missing from the header or query.
 */
export class GetCountriesLibraryUnauthorized extends Error {
  errors?: Array<GetCountriesLibraryLibraryErrors> | undefined;
  /**
   * Raw HTTP response; suitable for custom response parsing
   */
  rawResponse?: Response | undefined;

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

  constructor(err: GetCountriesLibraryUnauthorizedData) {
    const message = "message" in err && typeof err.message === "string"
      ? err.message
      : `API error occurred: ${JSON.stringify(err)}`;
    super(message);
    this.data$ = err;

    if (err.errors != null) this.errors = err.errors;
    if (err.rawResponse != null) this.rawResponse = err.rawResponse;

    this.name = "GetCountriesLibraryUnauthorized";
  }
}

export type GetCountriesLibraryErrors = {
  code?: number | undefined;
  message?: string | undefined;
  status?: number | undefined;
};

/**
 * Bad Request - A parameter was not specified, or was specified incorrectly.
 */
export type GetCountriesLibraryBadRequestData = {
  errors?: Array<GetCountriesLibraryErrors> | undefined;
  /**
   * Raw HTTP response; suitable for custom response parsing
   */
  rawResponse?: Response | undefined;
};

/**
 * Bad Request - A parameter was not specified, or was specified incorrectly.
 */
export class GetCountriesLibraryBadRequest extends Error {
  errors?: Array<GetCountriesLibraryErrors> | undefined;
  /**
   * Raw HTTP response; suitable for custom response parsing
   */
  rawResponse?: Response | undefined;

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

  constructor(err: GetCountriesLibraryBadRequestData) {
    const message = "message" in err && typeof err.message === "string"
      ? err.message
      : `API error occurred: ${JSON.stringify(err)}`;
    super(message);
    this.data$ = err;

    if (err.errors != null) this.errors = err.errors;
    if (err.rawResponse != null) this.rawResponse = err.rawResponse;

    this.name = "GetCountriesLibraryBadRequest";
  }
}

/** @internal */
export const GetCountriesLibraryLibraryErrors$inboundSchema: z.ZodType<
  GetCountriesLibraryLibraryErrors,
  z.ZodTypeDef,
  unknown
> = z.object({
  code: z.number().int().optional(),
  message: z.string().optional(),
  status: z.number().int().optional(),
});

/** @internal */
export type GetCountriesLibraryLibraryErrors$Outbound = {
  code?: number | undefined;
  message?: string | undefined;
  status?: number | undefined;
};

/** @internal */
export const GetCountriesLibraryLibraryErrors$outboundSchema: z.ZodType<
  GetCountriesLibraryLibraryErrors$Outbound,
  z.ZodTypeDef,
  GetCountriesLibraryLibraryErrors
> = z.object({
  code: z.number().int().optional(),
  message: z.string().optional(),
  status: z.number().int().optional(),
});

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

export function getCountriesLibraryLibraryErrorsToJSON(
  getCountriesLibraryLibraryErrors: GetCountriesLibraryLibraryErrors,
): string {
  return JSON.stringify(
    GetCountriesLibraryLibraryErrors$outboundSchema.parse(
      getCountriesLibraryLibraryErrors,
    ),
  );
}

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

/** @internal */
export const GetCountriesLibraryUnauthorized$inboundSchema: z.ZodType<
  GetCountriesLibraryUnauthorized,
  z.ZodTypeDef,
  unknown
> = z.object({
  errors: z.array(z.lazy(() => GetCountriesLibraryLibraryErrors$inboundSchema))
    .optional(),
  RawResponse: z.instanceof(Response).optional(),
})
  .transform((v) => {
    const remapped = remap$(v, {
      "RawResponse": "rawResponse",
    });

    return new GetCountriesLibraryUnauthorized(remapped);
  });

/** @internal */
export type GetCountriesLibraryUnauthorized$Outbound = {
  errors?: Array<GetCountriesLibraryLibraryErrors$Outbound> | undefined;
  RawResponse?: never | undefined;
};

/** @internal */
export const GetCountriesLibraryUnauthorized$outboundSchema: z.ZodType<
  GetCountriesLibraryUnauthorized$Outbound,
  z.ZodTypeDef,
  GetCountriesLibraryUnauthorized
> = z.instanceof(GetCountriesLibraryUnauthorized)
  .transform(v => v.data$)
  .pipe(
    z.object({
      errors: z.array(
        z.lazy(() => GetCountriesLibraryLibraryErrors$outboundSchema),
      ).optional(),
      rawResponse: z.instanceof(Response).transform(() => {
        throw new Error("Response cannot be serialized");
      }).optional(),
    }).transform((v) => {
      return remap$(v, {
        rawResponse: "RawResponse",
      });
    }),
  );

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

/** @internal */
export const GetCountriesLibraryErrors$inboundSchema: z.ZodType<
  GetCountriesLibraryErrors,
  z.ZodTypeDef,
  unknown
> = z.object({
  code: z.number().int().optional(),
  message: z.string().optional(),
  status: z.number().int().optional(),
});

/** @internal */
export type GetCountriesLibraryErrors$Outbound = {
  code?: number | undefined;
  message?: string | undefined;
  status?: number | undefined;
};

/** @internal */
export const GetCountriesLibraryErrors$outboundSchema: z.ZodType<
  GetCountriesLibraryErrors$Outbound,
  z.ZodTypeDef,
  GetCountriesLibraryErrors
> = z.object({
  code: z.number().int().optional(),
  message: z.string().optional(),
  status: z.number().int().optional(),
});

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

export function getCountriesLibraryErrorsToJSON(
  getCountriesLibraryErrors: GetCountriesLibraryErrors,
): string {
  return JSON.stringify(
    GetCountriesLibraryErrors$outboundSchema.parse(getCountriesLibraryErrors),
  );
}

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

/** @internal */
export const GetCountriesLibraryBadRequest$inboundSchema: z.ZodType<
  GetCountriesLibraryBadRequest,
  z.ZodTypeDef,
  unknown
> = z.object({
  errors: z.array(z.lazy(() => GetCountriesLibraryErrors$inboundSchema))
    .optional(),
  RawResponse: z.instanceof(Response).optional(),
})
  .transform((v) => {
    const remapped = remap$(v, {
      "RawResponse": "rawResponse",
    });

    return new GetCountriesLibraryBadRequest(remapped);
  });

/** @internal */
export type GetCountriesLibraryBadRequest$Outbound = {
  errors?: Array<GetCountriesLibraryErrors$Outbound> | undefined;
  RawResponse?: never | undefined;
};

/** @internal */
export const GetCountriesLibraryBadRequest$outboundSchema: z.ZodType<
  GetCountriesLibraryBadRequest$Outbound,
  z.ZodTypeDef,
  GetCountriesLibraryBadRequest
> = z.instanceof(GetCountriesLibraryBadRequest)
  .transform(v => v.data$)
  .pipe(
    z.object({
      errors: z.array(z.lazy(() => GetCountriesLibraryErrors$outboundSchema))
        .optional(),
      rawResponse: z.instanceof(Response).transform(() => {
        throw new Error("Response cannot be serialized");
      }).optional(),
    }).transform((v) => {
      return remap$(v, {
        rawResponse: "RawResponse",
      });
    }),
  );

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