/*
 * 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";

export type Target = {
  /**
   * A list of the keys for targets that will receive this variation because of individual targeting
   */
  values: Array<string>;
  /**
   * The index, from the array of variations for this flag, of the variation to serve this list of targets
   */
  variation: number;
  /**
   * The context kind of the individual target
   */
  contextKind?: string | undefined;
};

/** @internal */
export const Target$inboundSchema: z.ZodType<Target, z.ZodTypeDef, unknown> = z
  .object({
    values: z.array(z.string()),
    variation: z.number().int(),
    contextKind: z.string().optional(),
  });
/** @internal */
export type Target$Outbound = {
  values: Array<string>;
  variation: number;
  contextKind?: string | undefined;
};

/** @internal */
export const Target$outboundSchema: z.ZodType<
  Target$Outbound,
  z.ZodTypeDef,
  Target
> = z.object({
  values: z.array(z.string()),
  variation: z.number().int(),
  contextKind: z.string().optional(),
});

export function targetToJSON(target: Target): string {
  return JSON.stringify(Target$outboundSchema.parse(target));
}
export function targetFromJSON(
  jsonString: string,
): SafeParseResult<Target, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Target$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Target' from JSON`,
  );
}
