/*
 * 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 GetServerCapabilitiesErrors = {
  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 GetServerCapabilitiesUnauthorizedData = {
  errors?: Array<GetServerCapabilitiesErrors> | 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 GetServerCapabilitiesUnauthorized extends Error {
  errors?: Array<GetServerCapabilitiesErrors> | undefined;
  /**
   * Raw HTTP response; suitable for custom response parsing
   */
  rawResponse?: Response | undefined;

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

  constructor(err: GetServerCapabilitiesUnauthorizedData) {
    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 = "GetServerCapabilitiesUnauthorized";
  }
}

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

/**
 * Bad Request - A parameter was not specified, or was specified incorrectly.
 */
export type GetServerCapabilitiesBadRequestData = {
  errors?: Array<Errors> | 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 GetServerCapabilitiesBadRequest extends Error {
  errors?: Array<Errors> | undefined;
  /**
   * Raw HTTP response; suitable for custom response parsing
   */
  rawResponse?: Response | undefined;

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

  constructor(err: GetServerCapabilitiesBadRequestData) {
    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 = "GetServerCapabilitiesBadRequest";
  }
}

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

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

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

export function getServerCapabilitiesErrorsToJSON(
  getServerCapabilitiesErrors: GetServerCapabilitiesErrors,
): string {
  return JSON.stringify(
    GetServerCapabilitiesErrors$outboundSchema.parse(
      getServerCapabilitiesErrors,
    ),
  );
}

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

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

    return new GetServerCapabilitiesUnauthorized(remapped);
  });

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

/** @internal */
export const GetServerCapabilitiesUnauthorized$outboundSchema: z.ZodType<
  GetServerCapabilitiesUnauthorized$Outbound,
  z.ZodTypeDef,
  GetServerCapabilitiesUnauthorized
> = z.instanceof(GetServerCapabilitiesUnauthorized)
  .transform(v => v.data$)
  .pipe(
    z.object({
      errors: z.array(z.lazy(() => GetServerCapabilitiesErrors$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 GetServerCapabilitiesUnauthorized$ {
  /** @deprecated use `GetServerCapabilitiesUnauthorized$inboundSchema` instead. */
  export const inboundSchema = GetServerCapabilitiesUnauthorized$inboundSchema;
  /** @deprecated use `GetServerCapabilitiesUnauthorized$outboundSchema` instead. */
  export const outboundSchema =
    GetServerCapabilitiesUnauthorized$outboundSchema;
  /** @deprecated use `GetServerCapabilitiesUnauthorized$Outbound` instead. */
  export type Outbound = GetServerCapabilitiesUnauthorized$Outbound;
}

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

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

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

export function errorsToJSON(errors: Errors): string {
  return JSON.stringify(Errors$outboundSchema.parse(errors));
}

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

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

    return new GetServerCapabilitiesBadRequest(remapped);
  });

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

/** @internal */
export const GetServerCapabilitiesBadRequest$outboundSchema: z.ZodType<
  GetServerCapabilitiesBadRequest$Outbound,
  z.ZodTypeDef,
  GetServerCapabilitiesBadRequest
> = z.instanceof(GetServerCapabilitiesBadRequest)
  .transform(v => v.data$)
  .pipe(
    z.object({
      errors: z.array(z.lazy(() => Errors$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 GetServerCapabilitiesBadRequest$ {
  /** @deprecated use `GetServerCapabilitiesBadRequest$inboundSchema` instead. */
  export const inboundSchema = GetServerCapabilitiesBadRequest$inboundSchema;
  /** @deprecated use `GetServerCapabilitiesBadRequest$outboundSchema` instead. */
  export const outboundSchema = GetServerCapabilitiesBadRequest$outboundSchema;
  /** @deprecated use `GetServerCapabilitiesBadRequest$Outbound` instead. */
  export type Outbound = GetServerCapabilitiesBadRequest$Outbound;
}
