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

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

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

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

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

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

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

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

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

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

export function createPlaylistPlaylistsErrorsToJSON(
  createPlaylistPlaylistsErrors: CreatePlaylistPlaylistsErrors,
): string {
  return JSON.stringify(
    CreatePlaylistPlaylistsErrors$outboundSchema.parse(
      createPlaylistPlaylistsErrors,
    ),
  );
}

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

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

    return new CreatePlaylistUnauthorized(remapped);
  });

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

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

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

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

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

export function createPlaylistErrorsToJSON(
  createPlaylistErrors: CreatePlaylistErrors,
): string {
  return JSON.stringify(
    CreatePlaylistErrors$outboundSchema.parse(createPlaylistErrors),
  );
}

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

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

    return new CreatePlaylistBadRequest(remapped);
  });

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

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