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

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

export type Headers = {};

export const StageRoutesType = {
  Host: "host",
  Header: "header",
  Cookie: "cookie",
  Query: "query",
} as const;
export type StageRoutesType = ClosedEnum<typeof StageRoutesType>;

export type Has = {
  type?: StageRoutesType | undefined;
  key?: string | undefined;
  value?: string | undefined;
};

export const StageRoutesProjectRoutesType = {
  Host: "host",
  Header: "header",
  Cookie: "cookie",
  Query: "query",
} as const;
export type StageRoutesProjectRoutesType = ClosedEnum<
  typeof StageRoutesProjectRoutesType
>;

export type Missing = {
  type?: StageRoutesProjectRoutesType | undefined;
  key?: string | undefined;
  value?: string | undefined;
};

export const StageRoutesProjectRoutesRequestType = {
  RequestHeaders: "request.headers",
  RequestQuery: "request.query",
  ResponseHeaders: "response.headers",
} as const;
export type StageRoutesProjectRoutesRequestType = ClosedEnum<
  typeof StageRoutesProjectRoutesRequestType
>;

export const Op = {
  Append: "append",
  Set: "set",
  Delete: "delete",
} as const;
export type Op = ClosedEnum<typeof Op>;

export type StageRoutesTarget = {};

export type StageRoutesTransforms = {
  type?: StageRoutesProjectRoutesRequestType | undefined;
  op?: Op | undefined;
  target?: StageRoutesTarget | undefined;
  args?: any | undefined;
  env?: Array<string> | undefined;
};

export type Route = {
  src: string;
  dest?: string | undefined;
  headers?: Headers | undefined;
  caseSensitive?: boolean | undefined;
  status?: number | undefined;
  has?: Array<Has> | undefined;
  missing?: Array<Missing> | undefined;
  transforms?: Array<StageRoutesTransforms> | undefined;
  respectOriginCacheControl?: boolean | undefined;
};

export type Routes = {
  id: string;
  name: string;
  description?: string | undefined;
  enabled?: boolean | undefined;
  route: Route;
};

export type StageRoutesRequestBody = {
  overwrite?: boolean | undefined;
  routes?: Array<Routes> | undefined;
};

export type StageRoutesRequest = {
  projectId: string;
  /**
   * 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;
  requestBody?: StageRoutesRequestBody | undefined;
};

/**
 * A version of routing rules stored in S3.
 */
export type StageRoutesVersion = {
  /**
   * Unique identifier for the version.
   */
  id: string;
  /**
   * The S3 key where the routing rules are stored.
   */
  s3Key: string;
  /**
   * Timestamp of when this version was last modified.
   */
  lastModified: number;
  /**
   * The user who created this version.
   */
  createdBy: string;
  /**
   * Whether this version is staged and not yet promoted to production.
   */
  isStaging?: boolean | undefined;
  /**
   * Whether this version is currently live in production.
   */
  isLive?: boolean | undefined;
  /**
   * The number of routing rules in this version.
   */
  ruleCount?: number | undefined;
  /**
   * The staging alias for previewing this version.
   */
  alias?: string | undefined;
};

export type StageRoutesResponseBody = {
  /**
   * A version of routing rules stored in S3.
   */
  version: StageRoutesVersion;
};

/** @internal */
export type Headers$Outbound = {};

/** @internal */
export const Headers$outboundSchema: z.ZodType<
  Headers$Outbound,
  z.ZodTypeDef,
  Headers
> = z.object({});

export function headersToJSON(headers: Headers): string {
  return JSON.stringify(Headers$outboundSchema.parse(headers));
}

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

/** @internal */
export type Has$Outbound = {
  type?: string | undefined;
  key?: string | undefined;
  value?: string | undefined;
};

/** @internal */
export const Has$outboundSchema: z.ZodType<Has$Outbound, z.ZodTypeDef, Has> = z
  .object({
    type: StageRoutesType$outboundSchema.optional(),
    key: z.string().optional(),
    value: z.string().optional(),
  });

export function hasToJSON(has: Has): string {
  return JSON.stringify(Has$outboundSchema.parse(has));
}

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

/** @internal */
export type Missing$Outbound = {
  type?: string | undefined;
  key?: string | undefined;
  value?: string | undefined;
};

/** @internal */
export const Missing$outboundSchema: z.ZodType<
  Missing$Outbound,
  z.ZodTypeDef,
  Missing
> = z.object({
  type: StageRoutesProjectRoutesType$outboundSchema.optional(),
  key: z.string().optional(),
  value: z.string().optional(),
});

export function missingToJSON(missing: Missing): string {
  return JSON.stringify(Missing$outboundSchema.parse(missing));
}

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

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

/** @internal */
export type StageRoutesTarget$Outbound = {};

/** @internal */
export const StageRoutesTarget$outboundSchema: z.ZodType<
  StageRoutesTarget$Outbound,
  z.ZodTypeDef,
  StageRoutesTarget
> = z.object({});

export function stageRoutesTargetToJSON(
  stageRoutesTarget: StageRoutesTarget,
): string {
  return JSON.stringify(
    StageRoutesTarget$outboundSchema.parse(stageRoutesTarget),
  );
}

/** @internal */
export type StageRoutesTransforms$Outbound = {
  type?: string | undefined;
  op?: string | undefined;
  target?: StageRoutesTarget$Outbound | undefined;
  args?: any | undefined;
  env?: Array<string> | undefined;
};

/** @internal */
export const StageRoutesTransforms$outboundSchema: z.ZodType<
  StageRoutesTransforms$Outbound,
  z.ZodTypeDef,
  StageRoutesTransforms
> = z.object({
  type: StageRoutesProjectRoutesRequestType$outboundSchema.optional(),
  op: Op$outboundSchema.optional(),
  target: z.lazy(() => StageRoutesTarget$outboundSchema).optional(),
  args: z.any().optional(),
  env: z.array(z.string()).optional(),
});

export function stageRoutesTransformsToJSON(
  stageRoutesTransforms: StageRoutesTransforms,
): string {
  return JSON.stringify(
    StageRoutesTransforms$outboundSchema.parse(stageRoutesTransforms),
  );
}

/** @internal */
export type Route$Outbound = {
  src: string;
  dest?: string | undefined;
  headers?: Headers$Outbound | undefined;
  caseSensitive?: boolean | undefined;
  status?: number | undefined;
  has?: Array<Has$Outbound> | undefined;
  missing?: Array<Missing$Outbound> | undefined;
  transforms?: Array<StageRoutesTransforms$Outbound> | undefined;
  respectOriginCacheControl?: boolean | undefined;
};

/** @internal */
export const Route$outboundSchema: z.ZodType<
  Route$Outbound,
  z.ZodTypeDef,
  Route
> = z.object({
  src: z.string(),
  dest: z.string().optional(),
  headers: z.lazy(() => Headers$outboundSchema).optional(),
  caseSensitive: z.boolean().optional(),
  status: z.number().int().optional(),
  has: z.array(z.lazy(() => Has$outboundSchema)).optional(),
  missing: z.array(z.lazy(() => Missing$outboundSchema)).optional(),
  transforms: z.array(z.lazy(() => StageRoutesTransforms$outboundSchema))
    .optional(),
  respectOriginCacheControl: z.boolean().optional(),
});

export function routeToJSON(route: Route): string {
  return JSON.stringify(Route$outboundSchema.parse(route));
}

/** @internal */
export type Routes$Outbound = {
  id: string;
  name: string;
  description?: string | undefined;
  enabled?: boolean | undefined;
  route: Route$Outbound;
};

/** @internal */
export const Routes$outboundSchema: z.ZodType<
  Routes$Outbound,
  z.ZodTypeDef,
  Routes
> = z.object({
  id: z.string(),
  name: z.string(),
  description: z.string().optional(),
  enabled: z.boolean().optional(),
  route: z.lazy(() => Route$outboundSchema),
});

export function routesToJSON(routes: Routes): string {
  return JSON.stringify(Routes$outboundSchema.parse(routes));
}

/** @internal */
export type StageRoutesRequestBody$Outbound = {
  overwrite?: boolean | undefined;
  routes?: Array<Routes$Outbound> | undefined;
};

/** @internal */
export const StageRoutesRequestBody$outboundSchema: z.ZodType<
  StageRoutesRequestBody$Outbound,
  z.ZodTypeDef,
  StageRoutesRequestBody
> = z.object({
  overwrite: z.boolean().optional(),
  routes: z.array(z.lazy(() => Routes$outboundSchema)).optional(),
});

export function stageRoutesRequestBodyToJSON(
  stageRoutesRequestBody: StageRoutesRequestBody,
): string {
  return JSON.stringify(
    StageRoutesRequestBody$outboundSchema.parse(stageRoutesRequestBody),
  );
}

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

/** @internal */
export const StageRoutesRequest$outboundSchema: z.ZodType<
  StageRoutesRequest$Outbound,
  z.ZodTypeDef,
  StageRoutesRequest
> = z.object({
  projectId: z.string(),
  teamId: z.string().optional(),
  slug: z.string().optional(),
  requestBody: z.lazy(() => StageRoutesRequestBody$outboundSchema).optional(),
}).transform((v) => {
  return remap$(v, {
    requestBody: "RequestBody",
  });
});

export function stageRoutesRequestToJSON(
  stageRoutesRequest: StageRoutesRequest,
): string {
  return JSON.stringify(
    StageRoutesRequest$outboundSchema.parse(stageRoutesRequest),
  );
}

/** @internal */
export const StageRoutesVersion$inboundSchema: z.ZodType<
  StageRoutesVersion,
  z.ZodTypeDef,
  unknown
> = z.object({
  id: types.string(),
  s3Key: types.string(),
  lastModified: types.number(),
  createdBy: types.string(),
  isStaging: types.optional(types.boolean()),
  isLive: types.optional(types.boolean()),
  ruleCount: types.optional(types.number()),
  alias: types.optional(types.string()),
});

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

/** @internal */
export const StageRoutesResponseBody$inboundSchema: z.ZodType<
  StageRoutesResponseBody,
  z.ZodTypeDef,
  unknown
> = z.object({
  version: z.lazy(() => StageRoutesVersion$inboundSchema),
});

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