/*
 * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
 */

import * as z from "zod/v3";
import { remap as remap$ } from "../../lib/primitives.js";
import { safeParse } from "../../lib/schemas.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  Clause,
  Clause$inboundSchema,
  Clause$Outbound,
  Clause$outboundSchema,
} from "./clause.js";
import {
  Rollout,
  Rollout$inboundSchema,
  Rollout$Outbound,
  Rollout$outboundSchema,
} from "./rollout.js";

export type Rule = {
  /**
   * The flag rule ID
   */
  id?: string | undefined;
  /**
   * Whether the rule is disabled
   */
  disabled?: boolean | undefined;
  /**
   * The index of the variation, from the array of variations for this flag
   */
  variation?: number | undefined;
  rollout?: Rollout | undefined;
  /**
   * An array of clauses used for individual targeting based on attributes
   */
  clauses: Array<Clause>;
  /**
   * Whether the SDK sends a detailed event for every evaluation of this rule, instead of only summary counts. Required for Data Export, but doesn't enable Data Export on its own.
   */
  trackEvents: boolean;
  /**
   * The rule description
   */
  description?: string | undefined;
  ref?: string | undefined;
};

/** @internal */
export const Rule$inboundSchema: z.ZodType<Rule, z.ZodTypeDef, unknown> = z
  .object({
    _id: z.string().optional(),
    disabled: z.boolean().optional(),
    variation: z.number().int().optional(),
    rollout: Rollout$inboundSchema.optional(),
    clauses: z.array(Clause$inboundSchema),
    trackEvents: z.boolean(),
    description: z.string().optional(),
    ref: z.string().optional(),
  }).transform((v) => {
    return remap$(v, {
      "_id": "id",
    });
  });
/** @internal */
export type Rule$Outbound = {
  _id?: string | undefined;
  disabled?: boolean | undefined;
  variation?: number | undefined;
  rollout?: Rollout$Outbound | undefined;
  clauses: Array<Clause$Outbound>;
  trackEvents: boolean;
  description?: string | undefined;
  ref?: string | undefined;
};

/** @internal */
export const Rule$outboundSchema: z.ZodType<Rule$Outbound, z.ZodTypeDef, Rule> =
  z.object({
    id: z.string().optional(),
    disabled: z.boolean().optional(),
    variation: z.number().int().optional(),
    rollout: Rollout$outboundSchema.optional(),
    clauses: z.array(Clause$outboundSchema),
    trackEvents: z.boolean(),
    description: z.string().optional(),
    ref: z.string().optional(),
  }).transform((v) => {
    return remap$(v, {
      id: "_id",
    });
  });

export function ruleToJSON(rule: Rule): string {
  return JSON.stringify(Rule$outboundSchema.parse(rule));
}
export function ruleFromJSON(
  jsonString: string,
): SafeParseResult<Rule, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Rule$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Rule' from JSON`,
  );
}
