/*
 * 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 {
  CompletionChunk,
  CompletionChunk$inboundSchema,
} from "./completionchunk.js";

export type CompletionEvent = {
  data: CompletionChunk;
};

/** @internal */
export const CompletionEvent$inboundSchema: z.ZodType<
  CompletionEvent,
  z.ZodTypeDef,
  unknown
> = z.object({
  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(CompletionChunk$inboundSchema),
});

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