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

import * as z from "zod/v4";
import { safeParse } from "../../lib/schemas.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { smartUnion } from "../../types/smartUnion.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  ReferenceChunk,
  ReferenceChunk$inboundSchema,
  ReferenceChunk$Outbound,
  ReferenceChunk$outboundSchema,
} from "./referencechunk.js";
import {
  TextChunk,
  TextChunk$inboundSchema,
  TextChunk$Outbound,
  TextChunk$outboundSchema,
} from "./textchunk.js";
import {
  ToolReferenceChunk,
  ToolReferenceChunk$inboundSchema,
  ToolReferenceChunk$Outbound,
  ToolReferenceChunk$outboundSchema,
} from "./toolreferencechunk.js";

export type Thinking = ToolReferenceChunk | TextChunk | ReferenceChunk;

export type ThinkChunk = {
  type?: "thinking" | undefined;
  thinking: Array<ToolReferenceChunk | TextChunk | ReferenceChunk>;
  /**
   * Signature to replay some reasoning blocks across turns.
   */
  signature?: string | null | undefined;
  /**
   * Whether the thinking chunk is closed or not. Currently only used for prefixing.
   */
  closed?: boolean | undefined;
};

/** @internal */
export const Thinking$inboundSchema: z.ZodType<Thinking, unknown> = smartUnion([
  ToolReferenceChunk$inboundSchema,
  TextChunk$inboundSchema,
  ReferenceChunk$inboundSchema,
]);
/** @internal */
export type Thinking$Outbound =
  | ToolReferenceChunk$Outbound
  | TextChunk$Outbound
  | ReferenceChunk$Outbound;

/** @internal */
export const Thinking$outboundSchema: z.ZodType<Thinking$Outbound, Thinking> =
  smartUnion([
    ToolReferenceChunk$outboundSchema,
    TextChunk$outboundSchema,
    ReferenceChunk$outboundSchema,
  ]);

export function thinkingToJSON(thinking: Thinking): string {
  return JSON.stringify(Thinking$outboundSchema.parse(thinking));
}
export function thinkingFromJSON(
  jsonString: string,
): SafeParseResult<Thinking, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Thinking$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Thinking' from JSON`,
  );
}

/** @internal */
export const ThinkChunk$inboundSchema: z.ZodType<ThinkChunk, unknown> = z
  .object({
    type: z.literal("thinking").default("thinking"),
    thinking: z.array(
      smartUnion([
        ToolReferenceChunk$inboundSchema,
        TextChunk$inboundSchema,
        ReferenceChunk$inboundSchema,
      ]),
    ),
    signature: z.nullable(z.string()).optional(),
    closed: z.boolean().optional(),
  });
/** @internal */
export type ThinkChunk$Outbound = {
  type: "thinking";
  thinking: Array<
    ToolReferenceChunk$Outbound | TextChunk$Outbound | ReferenceChunk$Outbound
  >;
  signature?: string | null | undefined;
  closed?: boolean | undefined;
};

/** @internal */
export const ThinkChunk$outboundSchema: z.ZodType<
  ThinkChunk$Outbound,
  ThinkChunk
> = z.object({
  type: z.literal("thinking").default("thinking" as const),
  thinking: z.array(
    smartUnion([
      ToolReferenceChunk$outboundSchema,
      TextChunk$outboundSchema,
      ReferenceChunk$outboundSchema,
    ]),
  ),
  signature: z.nullable(z.string()).optional(),
  closed: z.boolean().optional(),
});

export function thinkChunkToJSON(thinkChunk: ThinkChunk): string {
  return JSON.stringify(ThinkChunk$outboundSchema.parse(thinkChunk));
}
export function thinkChunkFromJSON(
  jsonString: string,
): SafeParseResult<ThinkChunk, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => ThinkChunk$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'ThinkChunk' from JSON`,
  );
}
