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

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 {
  TranscriptionStreamDone,
  TranscriptionStreamDone$inboundSchema,
} from "./transcriptionstreamdone.js";
import {
  TranscriptionStreamEventTypes,
  TranscriptionStreamEventTypes$inboundSchema,
} from "./transcriptionstreameventtypes.js";
import {
  TranscriptionStreamLanguage,
  TranscriptionStreamLanguage$inboundSchema,
} from "./transcriptionstreamlanguage.js";
import {
  TranscriptionStreamSegmentDelta,
  TranscriptionStreamSegmentDelta$inboundSchema,
} from "./transcriptionstreamsegmentdelta.js";
import {
  TranscriptionStreamTextDelta,
  TranscriptionStreamTextDelta$inboundSchema,
} from "./transcriptionstreamtextdelta.js";

export type TranscriptionStreamEventsData =
  | TranscriptionStreamDone
  | TranscriptionStreamLanguage
  | TranscriptionStreamSegmentDelta
  | TranscriptionStreamTextDelta
  | discriminatedUnionTypes.Unknown<"type">;

export type TranscriptionStreamEvents = {
  event: TranscriptionStreamEventTypes;
  data:
    | TranscriptionStreamDone
    | TranscriptionStreamLanguage
    | TranscriptionStreamSegmentDelta
    | TranscriptionStreamTextDelta
    | discriminatedUnionTypes.Unknown<"type">;
};

/** @internal */
export const TranscriptionStreamEventsData$inboundSchema: z.ZodType<
  TranscriptionStreamEventsData,
  unknown
> = discriminatedUnion("type", {
  ["transcription.done"]: TranscriptionStreamDone$inboundSchema,
  ["transcription.language"]: TranscriptionStreamLanguage$inboundSchema,
  ["transcription.segment"]: TranscriptionStreamSegmentDelta$inboundSchema,
  ["transcription.text.delta"]: TranscriptionStreamTextDelta$inboundSchema,
});

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

/** @internal */
export const TranscriptionStreamEvents$inboundSchema: z.ZodType<
  TranscriptionStreamEvents,
  unknown
> = z.object({
  event: TranscriptionStreamEventTypes$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", {
    ["transcription.done"]: TranscriptionStreamDone$inboundSchema,
    ["transcription.language"]: TranscriptionStreamLanguage$inboundSchema,
    ["transcription.segment"]: TranscriptionStreamSegmentDelta$inboundSchema,
    ["transcription.text.delta"]: TranscriptionStreamTextDelta$inboundSchema,
  })),
});

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