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

import * as z from "zod/v4";
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";
import {
  FilterCondition,
  FilterCondition$inboundSchema,
  FilterCondition$Outbound,
  FilterCondition$outboundSchema,
} from "./filtercondition.js";
import {
  FilterGroup,
  FilterGroup$inboundSchema,
  FilterGroup$Outbound,
  FilterGroup$outboundSchema,
} from "./filtergroup.js";

export type Filters = FilterCondition | FilterGroup;

export type FilterPayload = {
  filters: FilterCondition | FilterGroup | null;
};

/** @internal */
export const Filters$inboundSchema: z.ZodType<Filters, unknown> = smartUnion([
  FilterCondition$inboundSchema,
  FilterGroup$inboundSchema,
]);
/** @internal */
export type Filters$Outbound = FilterCondition$Outbound | FilterGroup$Outbound;

/** @internal */
export const Filters$outboundSchema: z.ZodType<Filters$Outbound, Filters> =
  smartUnion([FilterCondition$outboundSchema, FilterGroup$outboundSchema]);

export function filtersToJSON(filters: Filters): string {
  return JSON.stringify(Filters$outboundSchema.parse(filters));
}
export function filtersFromJSON(
  jsonString: string,
): SafeParseResult<Filters, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Filters$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Filters' from JSON`,
  );
}

/** @internal */
export const FilterPayload$inboundSchema: z.ZodType<FilterPayload, unknown> = z
  .object({
    filters: z.nullable(
      smartUnion([FilterCondition$inboundSchema, FilterGroup$inboundSchema]),
    ),
  });
/** @internal */
export type FilterPayload$Outbound = {
  filters: FilterCondition$Outbound | FilterGroup$Outbound | null;
};

/** @internal */
export const FilterPayload$outboundSchema: z.ZodType<
  FilterPayload$Outbound,
  FilterPayload
> = z.object({
  filters: z.nullable(
    smartUnion([FilterCondition$outboundSchema, FilterGroup$outboundSchema]),
  ),
});

export function filterPayloadToJSON(filterPayload: FilterPayload): string {
  return JSON.stringify(FilterPayload$outboundSchema.parse(filterPayload));
}
export function filterPayloadFromJSON(
  jsonString: string,
): SafeParseResult<FilterPayload, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => FilterPayload$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'FilterPayload' from JSON`,
  );
}
