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

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

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

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

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

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

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

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

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

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

export function getOnDeckLibraryErrorsToJSON(
  getOnDeckLibraryErrors: GetOnDeckLibraryErrors,
): string {
  return JSON.stringify(
    GetOnDeckLibraryErrors$outboundSchema.parse(getOnDeckLibraryErrors),
  );
}

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

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

    return new GetOnDeckUnauthorized(remapped);
  });

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

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

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

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

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

export function getOnDeckErrorsToJSON(
  getOnDeckErrors: GetOnDeckErrors,
): string {
  return JSON.stringify(GetOnDeckErrors$outboundSchema.parse(getOnDeckErrors));
}

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

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

    return new GetOnDeckBadRequest(remapped);
  });

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

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