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

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

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

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

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

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

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

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

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

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

export function getSearchResultsSearchErrorsToJSON(
  getSearchResultsSearchErrors: GetSearchResultsSearchErrors,
): string {
  return JSON.stringify(
    GetSearchResultsSearchErrors$outboundSchema.parse(
      getSearchResultsSearchErrors,
    ),
  );
}

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

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

    return new GetSearchResultsUnauthorized(remapped);
  });

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

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

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

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

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

export function getSearchResultsErrorsToJSON(
  getSearchResultsErrors: GetSearchResultsErrors,
): string {
  return JSON.stringify(
    GetSearchResultsErrors$outboundSchema.parse(getSearchResultsErrors),
  );
}

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

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

    return new GetSearchResultsBadRequest(remapped);
  });

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

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