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

import * as z from "zod/v4";
import { EventStream } from "../../lib/event-streams.js";
import { remap as remap$ } from "../../lib/primitives.js";
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 { smartUnion } from "../../types/smartUnion.js";
import * as components from "../components/index.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";

export type Data =
  | components.SpeechStreamAudioDelta
  | components.SpeechStreamDone
  | discriminatedUnionTypes.Unknown<"type">;

/**
 * Speech audio data.
 */
export type SpeechStreamEvents = {
  event: components.SpeechStreamEventTypes;
  data:
    | components.SpeechStreamAudioDelta
    | components.SpeechStreamDone
    | discriminatedUnionTypes.Unknown<"type">;
};

/**
 * Speech audio data.
 */
export type SpeechResponse = {
  /**
   * Base64 encoded audio data
   */
  audioData: string;
};

export type SpeechV1AudioSpeechPostResponse =
  | SpeechResponse
  | EventStream<SpeechStreamEvents>;

/** @internal */
export const Data$inboundSchema: z.ZodType<Data, unknown> = discriminatedUnion(
  "type",
  {
    ["speech.audio.delta"]: components.SpeechStreamAudioDelta$inboundSchema,
    ["speech.audio.done"]: components.SpeechStreamDone$inboundSchema,
  },
);

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

/** @internal */
export const SpeechStreamEvents$inboundSchema: z.ZodType<
  SpeechStreamEvents,
  unknown
> = z.object({
  event: components.SpeechStreamEventTypes$inboundSchema,
  data: z.string().transform((v, ctx) => {
    try {
      return JSON.parse(v);
    } catch (err) {
      ctx.addIssue({
        input: v,
        code: "custom",
        message: `malformed json: ${err}`,
      });
      return z.NEVER;
    }
  }).pipe(
    discriminatedUnion("type", {
      ["speech.audio.delta"]: components.SpeechStreamAudioDelta$inboundSchema,
      ["speech.audio.done"]: components.SpeechStreamDone$inboundSchema,
    }),
  ),
});

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

/** @internal */
export const SpeechResponse$inboundSchema: z.ZodType<SpeechResponse, unknown> =
  z.object({
    audio_data: z.string(),
  }).transform((v) => {
    return remap$(v, {
      "audio_data": "audioData",
    });
  });

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

/** @internal */
export const SpeechV1AudioSpeechPostResponse$inboundSchema: z.ZodType<
  SpeechV1AudioSpeechPostResponse,
  unknown
> = smartUnion([
  z.lazy(() => SpeechResponse$inboundSchema),
  z.custom<ReadableStream<Uint8Array>>(x => x instanceof ReadableStream)
    .transform(stream => {
      return new EventStream(stream, rawEvent => {
        return {
          done: false,
          value: z.lazy(() => SpeechStreamEvents$inboundSchema).parse(rawEvent),
        };
      });
    }),
]);

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