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

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

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

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

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

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

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

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

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

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

export function getPlaylistPlaylistsErrorsToJSON(
  getPlaylistPlaylistsErrors: GetPlaylistPlaylistsErrors,
): string {
  return JSON.stringify(
    GetPlaylistPlaylistsErrors$outboundSchema.parse(getPlaylistPlaylistsErrors),
  );
}

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

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

    return new GetPlaylistUnauthorized(remapped);
  });

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

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

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

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

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

export function getPlaylistErrorsToJSON(
  getPlaylistErrors: GetPlaylistErrors,
): string {
  return JSON.stringify(
    GetPlaylistErrors$outboundSchema.parse(getPlaylistErrors),
  );
}

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

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

    return new GetPlaylistBadRequest(remapped);
  });

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

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