/*
 * 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 {
  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 & { type: "transcription.done" })
  | (TranscriptionStreamLanguage & { type: "transcription.language" })
  | (TranscriptionStreamSegmentDelta & { type: "transcription.segment" })
  | (TranscriptionStreamTextDelta & { type: "transcription.text.delta" });

export type TranscriptionStreamEvents = {
  event: TranscriptionStreamEventTypes;
  data:
    | (TranscriptionStreamDone & { type: "transcription.done" })
    | (TranscriptionStreamLanguage & { type: "transcription.language" })
    | (TranscriptionStreamSegmentDelta & { type: "transcription.segment" })
    | (TranscriptionStreamTextDelta & { type: "transcription.text.delta" });
};

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

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,
  z.ZodTypeDef,
  unknown
> = z.object({
  event: TranscriptionStreamEventTypes$inboundSchema,
  data: z.string().transform((v, ctx) => {
    try {
      return JSON.parse(v);
    } catch (err) {
      ctx.addIssue({ code: "custom", message: `malformed json: ${err}` });
      return z.NEVER;
    }
  }).pipe(
    z.union([
      TranscriptionStreamDone$inboundSchema.and(
        z.object({ type: z.literal("transcription.done") }),
      ),
      TranscriptionStreamLanguage$inboundSchema.and(
        z.object({ type: z.literal("transcription.language") }),
      ),
      TranscriptionStreamSegmentDelta$inboundSchema.and(
        z.object({ type: z.literal("transcription.segment") }),
      ),
      TranscriptionStreamTextDelta$inboundSchema.and(
        z.object({ type: z.literal("transcription.text.delta") }),
      ),
    ]),
  ),
});

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