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

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

export type GenerateFirewallRuleRequest = {
  projectId?: string | undefined;
  /**
   * The Team identifier to perform the request on behalf of.
   */
  teamId?: string | undefined;
  /**
   * The Team slug to perform the request on behalf of.
   */
  slug?: string | undefined;
};

export type GenerateFirewallRuleValue = string | number | Array<string>;

export type GenerateFirewallRuleConditions = {
  type: string;
  op: string;
  neg?: boolean | undefined;
  key?: string | undefined;
  value?: string | number | Array<string> | undefined;
};

export type GenerateFirewallRuleConditionGroup = {
  conditions: Array<GenerateFirewallRuleConditions>;
};

export type GenerateFirewallRuleRateLimit = {
  algo: string;
  window: number;
  limit: number;
  keys: Array<string>;
  action?: string | null | undefined;
};

export type GenerateFirewallRuleRedirect = {
  location: string;
  permanent: boolean;
};

export type GenerateFirewallRuleMitigate = {
  action: string;
  rateLimit?: GenerateFirewallRuleRateLimit | null | undefined;
  redirect?: GenerateFirewallRuleRedirect | null | undefined;
  actionDuration?: string | null | undefined;
};

export type GenerateFirewallRuleAction = {
  mitigate?: GenerateFirewallRuleMitigate | undefined;
};

export type GenerateFirewallRuleRule = {
  name: string;
  description?: string | undefined;
  active: boolean;
  conditionGroup: Array<GenerateFirewallRuleConditionGroup>;
  action: GenerateFirewallRuleAction;
};

export type GenerateFirewallRuleResponseBody = {
  rule?: GenerateFirewallRuleRule | undefined;
  error?: string | undefined;
};

/** @internal */
export type GenerateFirewallRuleRequest$Outbound = {
  projectId?: string | undefined;
  teamId?: string | undefined;
  slug?: string | undefined;
};

/** @internal */
export const GenerateFirewallRuleRequest$outboundSchema: z.ZodType<
  GenerateFirewallRuleRequest$Outbound,
  z.ZodTypeDef,
  GenerateFirewallRuleRequest
> = z.object({
  projectId: z.string().optional(),
  teamId: z.string().optional(),
  slug: z.string().optional(),
});

export function generateFirewallRuleRequestToJSON(
  generateFirewallRuleRequest: GenerateFirewallRuleRequest,
): string {
  return JSON.stringify(
    GenerateFirewallRuleRequest$outboundSchema.parse(
      generateFirewallRuleRequest,
    ),
  );
}

/** @internal */
export const GenerateFirewallRuleValue$inboundSchema: z.ZodType<
  GenerateFirewallRuleValue,
  z.ZodTypeDef,
  unknown
> = smartUnion([types.string(), types.number(), z.array(types.string())]);

export function generateFirewallRuleValueFromJSON(
  jsonString: string,
): SafeParseResult<GenerateFirewallRuleValue, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => GenerateFirewallRuleValue$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'GenerateFirewallRuleValue' from JSON`,
  );
}

/** @internal */
export const GenerateFirewallRuleConditions$inboundSchema: z.ZodType<
  GenerateFirewallRuleConditions,
  z.ZodTypeDef,
  unknown
> = z.object({
  type: types.string(),
  op: types.string(),
  neg: types.optional(types.boolean()),
  key: types.optional(types.string()),
  value: types.optional(
    smartUnion([types.string(), types.number(), z.array(types.string())]),
  ),
});

export function generateFirewallRuleConditionsFromJSON(
  jsonString: string,
): SafeParseResult<GenerateFirewallRuleConditions, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => GenerateFirewallRuleConditions$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'GenerateFirewallRuleConditions' from JSON`,
  );
}

/** @internal */
export const GenerateFirewallRuleConditionGroup$inboundSchema: z.ZodType<
  GenerateFirewallRuleConditionGroup,
  z.ZodTypeDef,
  unknown
> = z.object({
  conditions: z.array(
    z.lazy(() => GenerateFirewallRuleConditions$inboundSchema),
  ),
});

export function generateFirewallRuleConditionGroupFromJSON(
  jsonString: string,
): SafeParseResult<GenerateFirewallRuleConditionGroup, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) =>
      GenerateFirewallRuleConditionGroup$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'GenerateFirewallRuleConditionGroup' from JSON`,
  );
}

/** @internal */
export const GenerateFirewallRuleRateLimit$inboundSchema: z.ZodType<
  GenerateFirewallRuleRateLimit,
  z.ZodTypeDef,
  unknown
> = z.object({
  algo: types.string(),
  window: types.number(),
  limit: types.number(),
  keys: z.array(types.string()),
  action: z.nullable(types.string()).optional(),
});

export function generateFirewallRuleRateLimitFromJSON(
  jsonString: string,
): SafeParseResult<GenerateFirewallRuleRateLimit, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => GenerateFirewallRuleRateLimit$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'GenerateFirewallRuleRateLimit' from JSON`,
  );
}

/** @internal */
export const GenerateFirewallRuleRedirect$inboundSchema: z.ZodType<
  GenerateFirewallRuleRedirect,
  z.ZodTypeDef,
  unknown
> = z.object({
  location: types.string(),
  permanent: types.boolean(),
});

export function generateFirewallRuleRedirectFromJSON(
  jsonString: string,
): SafeParseResult<GenerateFirewallRuleRedirect, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => GenerateFirewallRuleRedirect$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'GenerateFirewallRuleRedirect' from JSON`,
  );
}

/** @internal */
export const GenerateFirewallRuleMitigate$inboundSchema: z.ZodType<
  GenerateFirewallRuleMitigate,
  z.ZodTypeDef,
  unknown
> = z.object({
  action: types.string(),
  rateLimit: z.nullable(
    z.lazy(() => GenerateFirewallRuleRateLimit$inboundSchema),
  ).optional(),
  redirect: z.nullable(z.lazy(() => GenerateFirewallRuleRedirect$inboundSchema))
    .optional(),
  actionDuration: z.nullable(types.string()).optional(),
});

export function generateFirewallRuleMitigateFromJSON(
  jsonString: string,
): SafeParseResult<GenerateFirewallRuleMitigate, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => GenerateFirewallRuleMitigate$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'GenerateFirewallRuleMitigate' from JSON`,
  );
}

/** @internal */
export const GenerateFirewallRuleAction$inboundSchema: z.ZodType<
  GenerateFirewallRuleAction,
  z.ZodTypeDef,
  unknown
> = z.object({
  mitigate: types.optional(
    z.lazy(() => GenerateFirewallRuleMitigate$inboundSchema),
  ),
});

export function generateFirewallRuleActionFromJSON(
  jsonString: string,
): SafeParseResult<GenerateFirewallRuleAction, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => GenerateFirewallRuleAction$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'GenerateFirewallRuleAction' from JSON`,
  );
}

/** @internal */
export const GenerateFirewallRuleRule$inboundSchema: z.ZodType<
  GenerateFirewallRuleRule,
  z.ZodTypeDef,
  unknown
> = z.object({
  name: types.string(),
  description: types.optional(types.string()),
  active: types.boolean(),
  conditionGroup: z.array(
    z.lazy(() => GenerateFirewallRuleConditionGroup$inboundSchema),
  ),
  action: z.lazy(() => GenerateFirewallRuleAction$inboundSchema),
});

export function generateFirewallRuleRuleFromJSON(
  jsonString: string,
): SafeParseResult<GenerateFirewallRuleRule, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => GenerateFirewallRuleRule$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'GenerateFirewallRuleRule' from JSON`,
  );
}

/** @internal */
export const GenerateFirewallRuleResponseBody$inboundSchema: z.ZodType<
  GenerateFirewallRuleResponseBody,
  z.ZodTypeDef,
  unknown
> = z.object({
  rule: types.optional(z.lazy(() => GenerateFirewallRuleRule$inboundSchema)),
  error: types.optional(types.string()),
});

export function generateFirewallRuleResponseBodyFromJSON(
  jsonString: string,
): SafeParseResult<GenerateFirewallRuleResponseBody, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => GenerateFirewallRuleResponseBody$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'GenerateFirewallRuleResponseBody' from JSON`,
  );
}
