/*
 * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
 */

import * as z from "zod/v3";
import { safeParse } from "../../lib/schemas.js";
import { ClosedEnum } from "../../types/enums.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  ClassifierJobOut,
  ClassifierJobOut$inboundSchema,
} from "./classifierjobout.js";
import {
  CompletionJobOut,
  CompletionJobOut$inboundSchema,
} from "./completionjobout.js";

export type JobsOutData =
  | (ClassifierJobOut & { jobType: "classifier" })
  | (CompletionJobOut & { jobType: "completion" });

export const JobsOutObject = {
  List: "list",
} as const;
export type JobsOutObject = ClosedEnum<typeof JobsOutObject>;

export type JobsOut = {
  data?:
    | Array<
      | (ClassifierJobOut & { jobType: "classifier" })
      | (CompletionJobOut & { jobType: "completion" })
    >
    | undefined;
  object: JobsOutObject | undefined;
  total: number;
};

/** @internal */
export const JobsOutData$inboundSchema: z.ZodType<
  JobsOutData,
  z.ZodTypeDef,
  unknown
> = z.union([
  ClassifierJobOut$inboundSchema.and(
    z.object({ job_type: z.literal("classifier") }).transform((v) => ({
      jobType: v.job_type,
    })),
  ),
  CompletionJobOut$inboundSchema.and(
    z.object({ job_type: z.literal("completion") }).transform((v) => ({
      jobType: v.job_type,
    })),
  ),
]);

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

/** @internal */
export const JobsOutObject$inboundSchema: z.ZodNativeEnum<
  typeof JobsOutObject
> = z.nativeEnum(JobsOutObject);

/** @internal */
export const JobsOut$inboundSchema: z.ZodType<JobsOut, z.ZodTypeDef, unknown> =
  z.object({
    data: z.array(
      z.union([
        ClassifierJobOut$inboundSchema.and(
          z.object({ job_type: z.literal("classifier") }).transform((v) => ({
            jobType: v.job_type,
          })),
        ),
        CompletionJobOut$inboundSchema.and(
          z.object({ job_type: z.literal("completion") }).transform((v) => ({
            jobType: v.job_type,
          })),
        ),
      ]),
    ).optional(),
    object: JobsOutObject$inboundSchema.default("list"),
    total: z.number().int(),
  });

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