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

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

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

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

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

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

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

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

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

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

export function getTopWatchedContentLibraryErrorsToJSON(
  getTopWatchedContentLibraryErrors: GetTopWatchedContentLibraryErrors,
): string {
  return JSON.stringify(
    GetTopWatchedContentLibraryErrors$outboundSchema.parse(
      getTopWatchedContentLibraryErrors,
    ),
  );
}

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

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

    return new GetTopWatchedContentUnauthorized(remapped);
  });

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

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

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

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

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

export function getTopWatchedContentErrorsToJSON(
  getTopWatchedContentErrors: GetTopWatchedContentErrors,
): string {
  return JSON.stringify(
    GetTopWatchedContentErrors$outboundSchema.parse(getTopWatchedContentErrors),
  );
}

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

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

    return new GetTopWatchedContentBadRequest(remapped);
  });

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

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