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

import * as z from "zod/v4";
import { remap as remap$ } from "../../lib/primitives.js";
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";

export type Or = FilterCondition | FilterGroup;

export type FilterGroup = {
  and?: Array<FilterCondition | FilterGroup> | null | undefined;
  or?: Array<FilterCondition | FilterGroup> | null | undefined;
};

export type And = FilterCondition | FilterGroup;

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

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

export function orToJSON(or: Or): string {
  return JSON.stringify(Or$outboundSchema.parse(or));
}
export function orFromJSON(
  jsonString: string,
): SafeParseResult<Or, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Or$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Or' from JSON`,
  );
}

/** @internal */
export const FilterGroup$inboundSchema: z.ZodType<FilterGroup, unknown> = z
  .object({
    AND: z.nullable(
      z.array(smartUnion([
        FilterCondition$inboundSchema,
        z.lazy(() => FilterGroup$inboundSchema),
      ])),
    ).optional(),
    OR: z.nullable(
      z.array(smartUnion([
        FilterCondition$inboundSchema,
        z.lazy(() => FilterGroup$inboundSchema),
      ])),
    ).optional(),
  }).transform((v) => {
    return remap$(v, {
      "AND": "and",
      "OR": "or",
    });
  });
/** @internal */
export type FilterGroup$Outbound = {
  AND?:
    | Array<FilterCondition$Outbound | FilterGroup$Outbound>
    | null
    | undefined;
  OR?:
    | Array<FilterCondition$Outbound | FilterGroup$Outbound>
    | null
    | undefined;
};

/** @internal */
export const FilterGroup$outboundSchema: z.ZodType<
  FilterGroup$Outbound,
  FilterGroup
> = z.object({
  and: z.nullable(
    z.array(smartUnion([
      FilterCondition$outboundSchema,
      z.lazy(() => FilterGroup$outboundSchema),
    ])),
  ).optional(),
  or: z.nullable(
    z.array(smartUnion([
      FilterCondition$outboundSchema,
      z.lazy(() => FilterGroup$outboundSchema),
    ])),
  ).optional(),
}).transform((v) => {
  return remap$(v, {
    and: "AND",
    or: "OR",
  });
});

export function filterGroupToJSON(filterGroup: FilterGroup): string {
  return JSON.stringify(FilterGroup$outboundSchema.parse(filterGroup));
}
export function filterGroupFromJSON(
  jsonString: string,
): SafeParseResult<FilterGroup, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => FilterGroup$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'FilterGroup' from JSON`,
  );
}

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

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

export function andToJSON(and: And): string {
  return JSON.stringify(And$outboundSchema.parse(and));
}
export function andFromJSON(
  jsonString: string,
): SafeParseResult<And, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => And$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'And' from JSON`,
  );
}
