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

import * as z from "zod/v4";
import { safeParse } from "../../lib/schemas.js";
import * as openEnums from "../../types/enums.js";
import { OpenEnum } from "../../types/enums.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { smartUnion } from "../../types/smartUnion.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  ToolProperties,
  ToolProperties$inboundSchema,
  ToolProperties$Outbound,
  ToolProperties$outboundSchema,
} from "./toolproperties.js";

export const LogicalExpressionType = {
  And: "and",
  Or: "or",
} as const;
export type LogicalExpressionType = OpenEnum<typeof LogicalExpressionType>;

export type LogicalExpression = {
  type: LogicalExpressionType;
  expressions: Array<LogicalExpression | ToolProperties | Array<string>>;
};

export type Expression = LogicalExpression | ToolProperties | Array<string>;

/** @internal */
export const LogicalExpressionType$inboundSchema: z.ZodType<
  LogicalExpressionType,
  unknown
> = openEnums.inboundSchema(LogicalExpressionType);
/** @internal */
export const LogicalExpressionType$outboundSchema: z.ZodType<
  string,
  LogicalExpressionType
> = openEnums.outboundSchema(LogicalExpressionType);

/** @internal */
export const LogicalExpression$inboundSchema: z.ZodType<
  LogicalExpression,
  unknown
> = z.object({
  type: LogicalExpressionType$inboundSchema,
  expressions: z.array(
    smartUnion([
      z.lazy(() => LogicalExpression$inboundSchema),
      ToolProperties$inboundSchema,
      z.array(z.string()),
    ]),
  ),
});
/** @internal */
export type LogicalExpression$Outbound = {
  type: string;
  expressions: Array<
    LogicalExpression$Outbound | ToolProperties$Outbound | Array<string>
  >;
};

/** @internal */
export const LogicalExpression$outboundSchema: z.ZodType<
  LogicalExpression$Outbound,
  LogicalExpression
> = z.object({
  type: LogicalExpressionType$outboundSchema,
  expressions: z.array(
    smartUnion([
      z.lazy(() => LogicalExpression$outboundSchema),
      ToolProperties$outboundSchema,
      z.array(z.string()),
    ]),
  ),
});

export function logicalExpressionToJSON(
  logicalExpression: LogicalExpression,
): string {
  return JSON.stringify(
    LogicalExpression$outboundSchema.parse(logicalExpression),
  );
}
export function logicalExpressionFromJSON(
  jsonString: string,
): SafeParseResult<LogicalExpression, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => LogicalExpression$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'LogicalExpression' from JSON`,
  );
}

/** @internal */
export const Expression$inboundSchema: z.ZodType<Expression, unknown> =
  smartUnion([
    z.lazy(() => LogicalExpression$inboundSchema),
    ToolProperties$inboundSchema,
    z.array(z.string()),
  ]);
/** @internal */
export type Expression$Outbound =
  | LogicalExpression$Outbound
  | ToolProperties$Outbound
  | Array<string>;

/** @internal */
export const Expression$outboundSchema: z.ZodType<
  Expression$Outbound,
  Expression
> = smartUnion([
  z.lazy(() => LogicalExpression$outboundSchema),
  ToolProperties$outboundSchema,
  z.array(z.string()),
]);

export function expressionToJSON(expression: Expression): string {
  return JSON.stringify(Expression$outboundSchema.parse(expression));
}
export function expressionFromJSON(
  jsonString: string,
): SafeParseResult<Expression, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Expression$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Expression' from JSON`,
  );
}
