/*
 * 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 {
  KeyCreditsRefill,
  KeyCreditsRefill$inboundSchema,
  KeyCreditsRefill$Outbound,
  KeyCreditsRefill$outboundSchema,
} from "./keycreditsrefill.js";

/**
 * Credit configuration and remaining balance for this key.
 */
export type KeyCreditsData = {
  /**
   * Number of credits remaining (null for unlimited).
   */
  remaining: number | null;
  /**
   * Configuration for automatic credit refill behavior.
   */
  refill?: KeyCreditsRefill | undefined;
};

/** @internal */
export const KeyCreditsData$inboundSchema: z.ZodType<
  KeyCreditsData,
  z.ZodTypeDef,
  unknown
> = z.object({
  remaining: z.nullable(z.number().int()),
  refill: KeyCreditsRefill$inboundSchema.optional(),
});
/** @internal */
export type KeyCreditsData$Outbound = {
  remaining: number | null;
  refill?: KeyCreditsRefill$Outbound | undefined;
};

/** @internal */
export const KeyCreditsData$outboundSchema: z.ZodType<
  KeyCreditsData$Outbound,
  z.ZodTypeDef,
  KeyCreditsData
> = z.object({
  remaining: z.nullable(z.number().int()),
  refill: KeyCreditsRefill$outboundSchema.optional(),
});

export function keyCreditsDataToJSON(keyCreditsData: KeyCreditsData): string {
  return JSON.stringify(KeyCreditsData$outboundSchema.parse(keyCreditsData));
}
export function keyCreditsDataFromJSON(
  jsonString: string,
): SafeParseResult<KeyCreditsData, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => KeyCreditsData$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'KeyCreditsData' from JSON`,
  );
}
