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

import * as z from "zod/v4";
import { remap as remap$ } from "../../lib/primitives.js";
import { safeParse } from "../../lib/schemas.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { smartUnion } from "../../types/smartUnion.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";

export type ChatCompletionEventPreviewExtraFields =
  | boolean
  | number
  | number
  | string
  | Date
  | Array<string>
  | { [k: string]: string };

export type ChatCompletionEventPreview = {
  eventId: string;
  correlationId: string;
  createdAt: Date;
  extraFields: {
    [k: string]: boolean | number | number | string | Date | Array<string> | {
      [k: string]: string;
    } | null;
  };
  nbInputTokens: number;
  nbOutputTokens: number;
};

/** @internal */
export const ChatCompletionEventPreviewExtraFields$inboundSchema: z.ZodType<
  ChatCompletionEventPreviewExtraFields,
  unknown
> = smartUnion([
  z.boolean(),
  z.int(),
  z.number(),
  z.string(),
  z.iso.datetime({ offset: true }).transform(v => new Date(v)),
  z.array(z.string()),
  z.record(z.string(), z.string()),
]);

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

/** @internal */
export const ChatCompletionEventPreview$inboundSchema: z.ZodType<
  ChatCompletionEventPreview,
  unknown
> = z.object({
  event_id: z.string(),
  correlation_id: z.string(),
  created_at: z.iso.datetime({ offset: true }).transform(v => new Date(v)),
  extra_fields: z.record(
    z.string(),
    z.nullable(
      smartUnion([
        z.boolean(),
        z.int(),
        z.number(),
        z.string(),
        z.iso.datetime({ offset: true }).transform(v => new Date(v)),
        z.array(z.string()),
        z.record(z.string(), z.string()),
      ]),
    ),
  ),
  nb_input_tokens: z.int(),
  nb_output_tokens: z.int(),
}).transform((v) => {
  return remap$(v, {
    "event_id": "eventId",
    "correlation_id": "correlationId",
    "created_at": "createdAt",
    "extra_fields": "extraFields",
    "nb_input_tokens": "nbInputTokens",
    "nb_output_tokens": "nbOutputTokens",
  });
});

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