/*
 * 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 * as components from "../components/index.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";

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

/**
 * OK
 */
export type JobsApiRoutesFineTuningCreateFineTuningJobResponse =
  | components.LegacyJobMetadataOut
  | (components.ClassifierJobOut & { jobType: "classifier" })
  | (components.CompletionJobOut & { jobType: "completion" });

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

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

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

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