/*
 * 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 {
  ConnectTriggerConfiguration,
  ConnectTriggerConfiguration$inboundSchema,
} from "./connecttriggerconfiguration.js";
import {
  ConnectTriggerDestination,
  ConnectTriggerDestination$inboundSchema,
} from "./connecttriggerdestination.js";
import { SDKValidationError } from "./sdkvalidationerror.js";

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

/**
 * Deployment environment of the project principal.
 */
export type Environment = string | Environment2;

/**
 * Principal that originally created the connector — either a Vercel user (interactive dashboard / CLI flow) or a Vercel deployment (OIDC-authenticated project, used by runtime auto-provisioning). See {@link ConnexPrincipal}. Optional: pre-existing rows from before this shape was introduced may carry no attribution at all.
 */
export type Two = {
  /**
   * Principal kind.
   */
  type: "project";
  /**
   * Vercel project ID.
   */
  id: string;
  /**
   * Deployment environment of the project principal.
   */
  environment: string | Environment2;
};

/**
 * Principal that originally created the connector — either a Vercel user (interactive dashboard / CLI flow) or a Vercel deployment (OIDC-authenticated project, used by runtime auto-provisioning). See {@link ConnexPrincipal}. Optional: pre-existing rows from before this shape was introduced may carry no attribution at all.
 */
export type One = {
  /**
   * Principal kind.
   */
  type: "user";
  /**
   * Vercel user ID.
   */
  id: string;
};

/**
 * Principal that created the connector.
 */
export type CreatedBy = One | Two;

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

/**
 * Deployment environment of the project principal.
 */
export type UpdatedByEnvironment = string | ConnectConnectorEnvironment2;

/**
 * Principal that most recently mutated the connector. Same shape as {@link createdBy} but tracks the most recent updater, not the original creator. At create time the two fields point at the same principal; they diverge on the first subsequent update.
 */
export type UpdatedBy2 = {
  /**
   * Principal kind.
   */
  type: "project";
  /**
   * Vercel project ID.
   */
  id: string;
  /**
   * Deployment environment of the project principal.
   */
  environment: string | ConnectConnectorEnvironment2;
};

/**
 * Principal that most recently mutated the connector. Same shape as {@link createdBy} but tracks the most recent updater, not the original creator. At create time the two fields point at the same principal; they diverge on the first subsequent update.
 */
export type UpdatedBy1 = {
  /**
   * Principal kind.
   */
  type: "user";
  /**
   * Vercel user ID.
   */
  id: string;
};

/**
 * Principal that most recently updated the connector.
 */
export type UpdatedBy = UpdatedBy1 | UpdatedBy2;

/**
 * How the connector row was originally created. New create paths stamp this explicitly; older rows may omit it.
 */
export const CreationMode = {
  Managed: "managed",
  Manual: "manual",
} as const;
/**
 * How the connector row was originally created. New create paths stamp this explicitly; older rows may omit it.
 */
export type CreationMode = ClosedEnum<typeof CreationMode>;

/**
 * Managed connector metadata exposed without leaking the manager connector or installation identifiers.
 */
export type Managed = {
  /**
   * Whether Vercel synchronizes provider-side configuration.
   */
  sync?: boolean | undefined;
};

/**
 * Connector implementation type.
 */
export const ConnectConnectorType = {
  ApiKey: "api-key",
  AwsAlpha: "aws-alpha",
  Custom: "custom",
  Discord: "discord",
  Github: "github",
  Linear: "linear",
  Linq: "linq",
  MicrosoftEntra: "microsoft-entra",
  MicrosoftTeams: "microsoft-teams",
  Oauth: "oauth",
  Photon: "photon",
  Salesforce: "salesforce",
  Sendblue: "sendblue",
  Slack: "slack",
  Snowflake: "snowflake",
  SnowflakeWif: "snowflake-wif",
} as const;
/**
 * Connector implementation type.
 */
export type ConnectConnectorType = ClosedEnum<typeof ConnectConnectorType>;

/**
 * App-token capabilities and known grants for the connector.
 */
export type AppTokens = {
  /**
   * Whether one app token can be used across installations.
   */
  crossInstallation: boolean;
  /**
   * Whether callers can narrow app-token grants per request.
   */
  supportsRefinement: boolean;
  /**
   * Whether callers can request resource-specific app tokens.
   */
  supportsResources?: boolean | undefined;
  /**
   * True when changing app token grants requires reinstalling the app, so tokens cannot be partitioned independently by requester environment.
   */
  requiresReinstallation?: boolean | undefined;
  /**
   * Known allowed app-level scopes. For Slack this is the bot scope set configured on the app; for OAuth it is the connector's enabled `clientCredentials.scopes` configuration.
   */
  scopes?: Array<string> | undefined;
  /**
   * Supported OAuth authorization-detail type names.
   */
  supportedAuthorizationDetails?: Array<string> | undefined;
  /**
   * Link to the page on the service where this connector's app-level permissions are declared and granted, when the service has one and it differs from `clientUrl`.
   */
  permissionsUrl?: string | undefined;
};

/**
 * User-token capabilities and known grants for the connector.
 */
export type UserTokens = {
  /**
   * Whether one user token can be used across installations.
   */
  crossInstallation: boolean;
  /**
   * Whether callers can narrow user-token grants per request.
   */
  supportsRefinement: boolean;
  /**
   * Whether callers can request resource-specific user tokens.
   */
  supportsResources?: boolean | undefined;
  /**
   * Known allowed user-level scopes. For Slack this is the user scope set configured on the app; for OAuth it is the connector's enabled `userAuthorization.scopes` configuration.
   */
  scopes?: Array<string> | undefined;
  /**
   * Supported OAuth authorization-detail type names.
   */
  supportedAuthorizationDetails?: Array<string> | undefined;
  /**
   * User authorization is completed by the Connect consent screen submitting a credential instead of an OAuth redirect.
   */
  manualCredentialInput?: boolean | undefined;
};

/**
 * Whether the connector icon can propagate to the provider.
 */
export const SupportsIcon = {
  False: "false",
  Maybe: "maybe",
  True: "true",
} as const;
/**
 * Whether the connector icon can propagate to the provider.
 */
export type SupportsIcon = ClosedEnum<typeof SupportsIcon>;

/**
 * A connector that defines how Vercel accesses an external service.
 */
export type ConnectConnector = {
  /**
   * Stable `scl_` connector ID. Use this value directly in `{connector}`.
   */
  id: string;
  /**
   * Team-scoped UID. URL-encode this value before using it in `{connector}`.
   */
  uid: string;
  /**
   * Installation used when a token request does not specify an installation.
   */
  defaultInstallationId?: string | undefined;
  /**
   * Creation time in epoch milliseconds.
   */
  createdAt: number;
  /**
   * Last update time in epoch milliseconds.
   */
  updatedAt: number;
  /**
   * Time when this connector started requiring reinstallation because an installation-affecting app-token grant changed.
   */
  reinstallAt?: number | undefined;
  /**
   * Principal that created the connector.
   */
  createdBy?: One | Two | undefined;
  /**
   * Principal that most recently updated the connector.
   */
  updatedBy?: UpdatedBy1 | UpdatedBy2 | undefined;
  /**
   * How the connector row was originally created. New create paths stamp this explicitly; older rows may omit it.
   */
  creationMode?: CreationMode | undefined;
  /**
   * Managed connector metadata exposed without leaking the manager connector or installation identifiers.
   */
  managed?: Managed | undefined;
  /**
   * Connector implementation type.
   */
  type: ConnectConnectorType;
  /**
   * Best-effort identifier of the third-party service this connector represents, independent of `type`. Examples: `'slack'`, `'mcp.linear.app'`, and `'auth.example.com'`. Always present in API responses.
   */
  service: string;
  /**
   * The connection method this connector was created from, when the create request named one.
   */
  connectionMethod?: string | undefined;
  /**
   * Which of the service's products/surfaces this connector points at.
   */
  target?: string | undefined;
  /**
   * Connector name within the owning team.
   */
  name: string;
  /**
   * Human-readable connector name.
   */
  displayName: string;
  /**
   * Provider-side URL for viewing or managing the resource represented by the connector. The destination can be an app, account, phone line, or service instance, depending on the connector type.
   */
  clientUrl?: string | null | undefined;
  /**
   * Redirect URI registered with the third-party service for this connector, if any. Used by `startAuthorization`/`startInstallation` to replay the exact URI back to the provider's token endpoint. Absent on connectors created before this field was introduced; those callers fall back to the `https://connect.vercel.com/callback` default.
   */
  redirectUri?: string | undefined;
  /**
   * Human-readable name of the connector type.
   */
  typeName: string;
  /**
   * Icon identifier supplied by the connector type.
   */
  typeIcon?: string | undefined;
  /**
   * Public website for the connected service.
   */
  website?: string | undefined;
  /**
   * Developer website for the connected service.
   */
  devsite?: string | undefined;
  /**
   * Developer documentation for the connected service.
   */
  docsite?: string | undefined;
  /**
   * Connector branding icon. SHA-1 hash that resolves to the uploaded icon through the Vercel avatar service. Consumers render this with `https://vercel.com/api/www/avatar/{icon}`.
   */
  icon?: string | undefined;
  /**
   * Hex background color (e.g., `#000000`) for branding.
   */
  backgroundColor?: string | undefined;
  /**
   * Hex accent color (e.g., `#000000`) for branding.
   */
  accentColor?: string | undefined;
  /**
   * Token subject types supported by the connector.
   */
  supportedSubjectTypes: Array<string>;
  /**
   * App-token capabilities and known grants for the connector.
   */
  appTokens?: AppTokens | undefined;
  /**
   * User-token capabilities and known grants for the connector.
   */
  userTokens?: UserTokens | undefined;
  /**
   * Whether the connector supports an installation flow.
   */
  supportsInstallation: boolean;
  /**
   * Whether Connect can revoke tokens for this connector.
   */
  supportsRevocation: boolean;
  /**
   * Whether this connector type supports trigger webhooks. Derived from the type definition; indicates that `triggers` and `triggerDestinations` may be meaningful for this connector.
   */
  supportsTriggers: boolean;
  /**
   * Whether the connector icon can propagate to the provider.
   */
  supportsIcon: SupportsIcon;
  /**
   * Incoming trigger configuration. Only present when enabled.
   */
  triggers?: ConnectTriggerConfiguration | undefined;
  /**
   * Known events this connector subscribes to (e.g. Slack bot events, GitHub webhook events). Names are type-specific and validated by the managed-create flow when forwarded to the third-party service.
   */
  events?: Array<string> | undefined;
  /**
   * Destinations that incoming triggers should be forwarded to. Limited to 3 entries. Set the initial destination with `triggerDestination` during creation. Replace the complete set with `PATCH /v1/connect/connectors/{connector}/trigger-destinations`.
   */
  triggerDestinations?: Array<ConnectTriggerDestination> | undefined;
};

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

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

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

/** @internal */
export const Two$inboundSchema: z.ZodType<Two, z.ZodTypeDef, unknown> = z
  .object({
    type: types.literal("project"),
    id: types.string(),
    environment: smartUnion([types.string(), Environment2$inboundSchema]),
  });

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

/** @internal */
export const One$inboundSchema: z.ZodType<One, z.ZodTypeDef, unknown> = z
  .object({
    type: types.literal("user"),
    id: types.string(),
  });

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

/** @internal */
export const CreatedBy$inboundSchema: z.ZodType<
  CreatedBy,
  z.ZodTypeDef,
  unknown
> = z.union([z.lazy(() => One$inboundSchema), z.lazy(() => Two$inboundSchema)]);

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

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

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

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

/** @internal */
export const UpdatedBy2$inboundSchema: z.ZodType<
  UpdatedBy2,
  z.ZodTypeDef,
  unknown
> = z.object({
  type: types.literal("project"),
  id: types.string(),
  environment: smartUnion([
    types.string(),
    ConnectConnectorEnvironment2$inboundSchema,
  ]),
});

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

/** @internal */
export const UpdatedBy1$inboundSchema: z.ZodType<
  UpdatedBy1,
  z.ZodTypeDef,
  unknown
> = z.object({
  type: types.literal("user"),
  id: types.string(),
});

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

/** @internal */
export const UpdatedBy$inboundSchema: z.ZodType<
  UpdatedBy,
  z.ZodTypeDef,
  unknown
> = z.union([
  z.lazy(() => UpdatedBy1$inboundSchema),
  z.lazy(() => UpdatedBy2$inboundSchema),
]);

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

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

/** @internal */
export const Managed$inboundSchema: z.ZodType<Managed, z.ZodTypeDef, unknown> =
  z.object({
    sync: types.optional(types.boolean()),
  });

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

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

/** @internal */
export const AppTokens$inboundSchema: z.ZodType<
  AppTokens,
  z.ZodTypeDef,
  unknown
> = z.object({
  crossInstallation: types.boolean(),
  supportsRefinement: types.boolean(),
  supportsResources: types.optional(types.boolean()),
  requiresReinstallation: types.optional(types.boolean()),
  scopes: types.optional(z.array(types.string())),
  supportedAuthorizationDetails: types.optional(z.array(types.string())),
  permissionsUrl: types.optional(types.string()),
});

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

/** @internal */
export const UserTokens$inboundSchema: z.ZodType<
  UserTokens,
  z.ZodTypeDef,
  unknown
> = z.object({
  crossInstallation: types.boolean(),
  supportsRefinement: types.boolean(),
  supportsResources: types.optional(types.boolean()),
  scopes: types.optional(z.array(types.string())),
  supportedAuthorizationDetails: types.optional(z.array(types.string())),
  manualCredentialInput: types.optional(types.boolean()),
});

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

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

/** @internal */
export const ConnectConnector$inboundSchema: z.ZodType<
  ConnectConnector,
  z.ZodTypeDef,
  unknown
> = z.object({
  id: types.string(),
  uid: types.string(),
  defaultInstallationId: types.optional(types.string()),
  createdAt: types.number(),
  updatedAt: types.number(),
  reinstallAt: types.optional(types.number()),
  createdBy: types.optional(
    z.union([z.lazy(() => One$inboundSchema), z.lazy(() => Two$inboundSchema)]),
  ),
  updatedBy: types.optional(
    z.union([
      z.lazy(() => UpdatedBy1$inboundSchema),
      z.lazy(() => UpdatedBy2$inboundSchema),
    ]),
  ),
  creationMode: types.optional(CreationMode$inboundSchema),
  managed: types.optional(z.lazy(() => Managed$inboundSchema)),
  type: ConnectConnectorType$inboundSchema,
  service: types.string(),
  connectionMethod: types.optional(types.string()),
  target: types.optional(types.string()),
  name: types.string(),
  displayName: types.string(),
  clientUrl: z.nullable(types.string()).optional(),
  redirectUri: types.optional(types.string()),
  typeName: types.string(),
  typeIcon: types.optional(types.string()),
  website: types.optional(types.string()),
  devsite: types.optional(types.string()),
  docsite: types.optional(types.string()),
  icon: types.optional(types.string()),
  backgroundColor: types.optional(types.string()),
  accentColor: types.optional(types.string()),
  supportedSubjectTypes: z.array(types.string()),
  appTokens: types.optional(z.lazy(() => AppTokens$inboundSchema)),
  userTokens: types.optional(z.lazy(() => UserTokens$inboundSchema)),
  supportsInstallation: types.boolean(),
  supportsRevocation: types.boolean(),
  supportsTriggers: types.boolean(),
  supportsIcon: SupportsIcon$inboundSchema,
  triggers: types.optional(ConnectTriggerConfiguration$inboundSchema),
  events: types.optional(z.array(types.string())),
  triggerDestinations: types.optional(
    z.array(ConnectTriggerDestination$inboundSchema),
  ),
});

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