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

import * as z from "zod/v4";
import { safeParse } from "../../lib/schemas.js";
import * as discriminatedUnionTypes from "../../types/discriminatedUnion.js";
import { discriminatedUnion } from "../../types/discriminatedUnion.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.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";

export type ContentChunk =
  | ImageURLChunk
  | TextChunk
  | (ReferenceChunk & { type: "reference" })
  | discriminatedUnionTypes.Unknown<"type">;

/** @internal */
export const ContentChunk$inboundSchema: z.ZodType<ContentChunk, unknown> =
  discriminatedUnion("type", {
    image_url: ImageURLChunk$inboundSchema,
    text: TextChunk$inboundSchema,
    reference: ReferenceChunk$inboundSchema.and(
      z.object({ type: z.literal("reference") }),
    ),
  });
/** @internal */
export type ContentChunk$Outbound =
  | ImageURLChunk$Outbound
  | TextChunk$Outbound
  | (ReferenceChunk$Outbound & { type: "reference" });

/** @internal */
export const ContentChunk$outboundSchema: z.ZodType<
  ContentChunk$Outbound,
  ContentChunk
> = z.union([
  ImageURLChunk$outboundSchema,
  TextChunk$outboundSchema,
  ReferenceChunk$outboundSchema.and(z.object({ type: z.literal("reference") })),
]);

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`,
  );
}
