/*
 * 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 UpdateRollingReleaseConfigRequest = {
  /**
   * Project ID or project name (URL-encoded)
   */
  idOrName: 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;
};

/**
 * A stage object configured for a rolling release once a new deployment is triggered the first stage will be read in the proxy for first time visitors, and if a RNG < targetPercentage then it will serve the new deployment. Upon approval the next stage will be read, etc.
 */
export type UpdateRollingReleaseConfigResponseBodyStages = {
  /**
   * The percentage of traffic to serve to the canary deployment (0-100)
   */
  targetPercentage: number;
  /**
   * Whether or not this stage requires manual approval to proceed
   */
  requireApproval?: boolean | undefined;
  /**
   * Duration in minutes for automatic advancement to the next stage
   */
  duration?: number | undefined;
  /**
   * Whether to linearly shift traffic over the duration of this stage
   */
  linearShift?: boolean | undefined;
};

export type UpdateRollingReleaseConfigResponseBodyRollingRelease = {
  stages?:
    | Array<UpdateRollingReleaseConfigResponseBodyStages>
    | null
    | undefined;
};

export type UpdateRollingReleaseConfigResponseBody2 = {
  rollingRelease: UpdateRollingReleaseConfigResponseBodyRollingRelease | null;
};

export type UpdateRollingReleaseConfigResponseBody1 = {
  rollingRelease?: any | null | undefined;
};

export type UpdateRollingReleaseConfigResponseBody =
  | UpdateRollingReleaseConfigResponseBody1
  | UpdateRollingReleaseConfigResponseBody2;

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

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

export function updateRollingReleaseConfigRequestToJSON(
  updateRollingReleaseConfigRequest: UpdateRollingReleaseConfigRequest,
): string {
  return JSON.stringify(
    UpdateRollingReleaseConfigRequest$outboundSchema.parse(
      updateRollingReleaseConfigRequest,
    ),
  );
}

/** @internal */
export const UpdateRollingReleaseConfigResponseBodyStages$inboundSchema:
  z.ZodType<
    UpdateRollingReleaseConfigResponseBodyStages,
    z.ZodTypeDef,
    unknown
  > = z.object({
    targetPercentage: types.number(),
    requireApproval: types.optional(types.boolean()),
    duration: types.optional(types.number()),
    linearShift: types.optional(types.boolean()),
  });

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

/** @internal */
export const UpdateRollingReleaseConfigResponseBodyRollingRelease$inboundSchema:
  z.ZodType<
    UpdateRollingReleaseConfigResponseBodyRollingRelease,
    z.ZodTypeDef,
    unknown
  > = z.object({
    stages: z.nullable(
      z.array(z.lazy(() =>
        UpdateRollingReleaseConfigResponseBodyStages$inboundSchema
      )),
    ).optional(),
  });

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

/** @internal */
export const UpdateRollingReleaseConfigResponseBody2$inboundSchema: z.ZodType<
  UpdateRollingReleaseConfigResponseBody2,
  z.ZodTypeDef,
  unknown
> = z.object({
  rollingRelease: types.nullable(
    z.lazy(() =>
      UpdateRollingReleaseConfigResponseBodyRollingRelease$inboundSchema
    ),
  ),
});

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

/** @internal */
export const UpdateRollingReleaseConfigResponseBody1$inboundSchema: z.ZodType<
  UpdateRollingReleaseConfigResponseBody1,
  z.ZodTypeDef,
  unknown
> = z.object({
  rollingRelease: z.nullable(z.any()).optional(),
});

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

/** @internal */
export const UpdateRollingReleaseConfigResponseBody$inboundSchema: z.ZodType<
  UpdateRollingReleaseConfigResponseBody,
  z.ZodTypeDef,
  unknown
> = smartUnion([
  z.lazy(() => UpdateRollingReleaseConfigResponseBody1$inboundSchema),
  z.lazy(() => UpdateRollingReleaseConfigResponseBody2$inboundSchema),
]);

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