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

import * as z from "zod/v3";
import { safeParse } from "../../lib/schemas.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 type JobsOut = {
  data?:
    | Array<
      | (ClassifierJobOut & { jobType: "classifier" })
      | (CompletionJobOut & { jobType: "completion" })
    >
    | undefined;
  object?: "list" | 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 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: z.literal("list").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`,
  );
}
