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

import * as z from "zod/v3";
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 { smartUnion } from "../types/smartUnion.js";
import { SDKValidationError } from "./sdkvalidationerror.js";

/**
 * Custom environments available on the project. This list can include environments where the connector is not enabled.
 */
export type CustomEnvironments = {
  /**
   * Stable custom environment ID.
   */
  id: string;
  /**
   * Current human-readable custom environment slug.
   */
  slug: string;
};

/**
 * Vercel project connected to the connector.
 */
export type Project = {
  /**
   * Same Vercel project ID as the connection's top-level `projectId`.
   */
  id: string;
  /**
   * Current Vercel project name.
   */
  name: string;
  /**
   * Custom environments available on the project. This list can include environments where the connector is not enabled.
   */
  customEnvironments?: Array<CustomEnvironments> | undefined;
};

export const EnabledEnvironments2 = {
  Development: "development",
  Preview: "preview",
  Production: "production",
} as const;
export type EnabledEnvironments2 = ClosedEnum<typeof EnabledEnvironments2>;

export type EnabledEnvironments = string | EnabledEnvironments2;

/**
 * A connection between a connector and a Vercel project, including the environments where the connector is enabled.
 */
export type ConnectProjectConnection = {
  /**
   * Stable `scl_` connector ID, even when the request used a UID.
   */
  connectorId: string;
  /**
   * Vercel project connected to the connector.
   */
  project: Project;
  /**
   * Environments where the connector is enabled for the project.
   */
  enabledEnvironments: Array<string | EnabledEnvironments2>;
  /**
   * Time when the project connection was created, in epoch milliseconds.
   */
  createdAt: number;
  /**
   * Time when the project connection was last updated, in epoch milliseconds.
   */
  updatedAt: number;
};

/** @internal */
export const CustomEnvironments$inboundSchema: z.ZodType<
  CustomEnvironments,
  z.ZodTypeDef,
  unknown
> = z.object({
  id: types.string(),
  slug: types.string(),
});

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

/** @internal */
export const Project$inboundSchema: z.ZodType<Project, z.ZodTypeDef, unknown> =
  z.object({
    id: types.string(),
    name: types.string(),
    customEnvironments: types.optional(
      z.array(z.lazy(() => CustomEnvironments$inboundSchema)),
    ),
  });

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

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

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

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

/** @internal */
export const ConnectProjectConnection$inboundSchema: z.ZodType<
  ConnectProjectConnection,
  z.ZodTypeDef,
  unknown
> = z.object({
  connectorId: types.string(),
  project: z.lazy(() => Project$inboundSchema),
  enabledEnvironments: z.array(
    smartUnion([types.string(), EnabledEnvironments2$inboundSchema]),
  ),
  createdAt: types.number(),
  updatedAt: types.number(),
});

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