/*
 * 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 {
  AudioChunk,
  AudioChunk$inboundSchema,
  AudioChunk$Outbound,
  AudioChunk$outboundSchema,
} from "./audiochunk.js";
import {
  DocumentURLChunk,
  DocumentURLChunk$inboundSchema,
  DocumentURLChunk$Outbound,
  DocumentURLChunk$outboundSchema,
} from "./documenturlchunk.js";
import {
  FileChunk,
  FileChunk$inboundSchema,
  FileChunk$Outbound,
  FileChunk$outboundSchema,
} from "./filechunk.js";
import {
  ImageURLChunk,
  ImageURLChunk$inboundSchema,
  ImageURLChunk$Outbound,
  ImageURLChunk$outboundSchema,
} from "./imageurlchunk.js";
import {
  ReferenceChunk,
  ReferenceChunk$inboundSchema,
  ReferenceChunk$Outbound,
  ReferenceChunk$outboundSchema,
} from "./referencechunk.js";
import {
  TextChunk,
  TextChunk$inboundSchema,
  TextChunk$Outbound,
  TextChunk$outboundSchema,
} from "./textchunk.js";
import {
  ThinkChunk,
  ThinkChunk$inboundSchema,
  ThinkChunk$Outbound,
  ThinkChunk$outboundSchema,
} from "./thinkchunk.js";

export type ContentChunk =
  | (ImageURLChunk & { type: "image_url" })
  | (DocumentURLChunk & { type: "document_url" })
  | (TextChunk & { type: "text" })
  | (ReferenceChunk & { type: "reference" })
  | (FileChunk & { type: "file" })
  | (ThinkChunk & { type: "thinking" })
  | (AudioChunk & { type: "input_audio" });

/** @internal */
export const ContentChunk$inboundSchema: z.ZodType<
  ContentChunk,
  z.ZodTypeDef,
  unknown
> = z.union([
  ImageURLChunk$inboundSchema.and(z.object({ type: z.literal("image_url") })),
  DocumentURLChunk$inboundSchema.and(
    z.object({ type: z.literal("document_url") }),
  ),
  TextChunk$inboundSchema.and(z.object({ type: z.literal("text") })),
  ReferenceChunk$inboundSchema.and(z.object({ type: z.literal("reference") })),
  FileChunk$inboundSchema.and(z.object({ type: z.literal("file") })),
  ThinkChunk$inboundSchema.and(z.object({ type: z.literal("thinking") })),
  AudioChunk$inboundSchema.and(z.object({ type: z.literal("input_audio") })),
]);
/** @internal */
export type ContentChunk$Outbound =
  | (ImageURLChunk$Outbound & { type: "image_url" })
  | (DocumentURLChunk$Outbound & { type: "document_url" })
  | (TextChunk$Outbound & { type: "text" })
  | (ReferenceChunk$Outbound & { type: "reference" })
  | (FileChunk$Outbound & { type: "file" })
  | (ThinkChunk$Outbound & { type: "thinking" })
  | (AudioChunk$Outbound & { type: "input_audio" });

/** @internal */
export const ContentChunk$outboundSchema: z.ZodType<
  ContentChunk$Outbound,
  z.ZodTypeDef,
  ContentChunk
> = z.union([
  ImageURLChunk$outboundSchema.and(z.object({ type: z.literal("image_url") })),
  DocumentURLChunk$outboundSchema.and(
    z.object({ type: z.literal("document_url") }),
  ),
  TextChunk$outboundSchema.and(z.object({ type: z.literal("text") })),
  ReferenceChunk$outboundSchema.and(z.object({ type: z.literal("reference") })),
  FileChunk$outboundSchema.and(z.object({ type: z.literal("file") })),
  ThinkChunk$outboundSchema.and(z.object({ type: z.literal("thinking") })),
  AudioChunk$outboundSchema.and(z.object({ type: z.literal("input_audio") })),
]);

export function contentChunkToJSON(contentChunk: ContentChunk): string {
  return JSON.stringify(ContentChunk$outboundSchema.parse(contentChunk));
}
export function contentChunkFromJSON(
  jsonString: string,
): SafeParseResult<ContentChunk, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => ContentChunk$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'ContentChunk' from JSON`,
  );
}
