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

import * as z from "zod/v3";
import { safeParse } from "../../lib/schemas.js";
import { ClosedEnum } from "../../types/enums.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";

/**
 * Filter type. One of [contextAttribute, eventProperty, group]
 */
export const FilterType = {
  Group: "group",
  ContextAttribute: "contextAttribute",
  EventProperty: "eventProperty",
} as const;
/**
 * Filter type. One of [contextAttribute, eventProperty, group]
 */
export type FilterType = ClosedEnum<typeof FilterType>;

export type Filter = {
  /**
   * Filter type. One of [contextAttribute, eventProperty, group]
   */
  type: FilterType;
  /**
   * If not a group node, the context attribute name or event property name to filter on
   */
  attribute?: string | undefined;
  op: string;
  /**
   * The context attribute / event property values or group member nodes
   */
  values: Array<any>;
  /**
   * For context attribute filters, the context kind.
   */
  contextKind?: string | undefined;
  /**
   * If set, then take the inverse of the operator. 'in' becomes 'not in'.
   */
  negate: boolean;
};

/** @internal */
export const FilterType$inboundSchema: z.ZodNativeEnum<typeof FilterType> = z
  .nativeEnum(FilterType);
/** @internal */
export const FilterType$outboundSchema: z.ZodNativeEnum<typeof FilterType> =
  FilterType$inboundSchema;

/** @internal */
export const Filter$inboundSchema: z.ZodType<Filter, z.ZodTypeDef, unknown> = z
  .object({
    type: FilterType$inboundSchema,
    attribute: z.string().optional(),
    op: z.string(),
    values: z.array(z.any()),
    contextKind: z.string().optional(),
    negate: z.boolean(),
  });
/** @internal */
export type Filter$Outbound = {
  type: string;
  attribute?: string | undefined;
  op: string;
  values: Array<any>;
  contextKind?: string | undefined;
  negate: boolean;
};

/** @internal */
export const Filter$outboundSchema: z.ZodType<
  Filter$Outbound,
  z.ZodTypeDef,
  Filter
> = z.object({
  type: FilterType$outboundSchema,
  attribute: z.string().optional(),
  op: z.string(),
  values: z.array(z.any()),
  contextKind: z.string().optional(),
  negate: z.boolean(),
});

export function filterToJSON(filter: Filter): string {
  return JSON.stringify(Filter$outboundSchema.parse(filter));
}
export function filterFromJSON(
  jsonString: string,
): SafeParseResult<Filter, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Filter$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Filter' from JSON`,
  );
}
