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

import * as z from "zod/v4";
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,
  unknown
> = z.object({
  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(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`,
  );
}
