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

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

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

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

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

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

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

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

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

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

export function getSearchLibraryLibraryErrorsToJSON(
  getSearchLibraryLibraryErrors: GetSearchLibraryLibraryErrors,
): string {
  return JSON.stringify(
    GetSearchLibraryLibraryErrors$outboundSchema.parse(
      getSearchLibraryLibraryErrors,
    ),
  );
}

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

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

    return new GetSearchLibraryUnauthorized(remapped);
  });

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

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

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

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

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

export function getSearchLibraryErrorsToJSON(
  getSearchLibraryErrors: GetSearchLibraryErrors,
): string {
  return JSON.stringify(
    GetSearchLibraryErrors$outboundSchema.parse(getSearchLibraryErrors),
  );
}

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

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

    return new GetSearchLibraryBadRequest(remapped);
  });

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

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