/*
 * 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 ListKmsIssuersRequest = {
  /**
   * Maximum number of issuers to return.
   */
  limit?: number | undefined;
  /**
   * Continuation cursor to retrieve the next page of results.
   */
  next?: 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 const ListKmsIssuersAlgorithm = {
  Es256: "ES256",
  Es384: "ES384",
  Es512: "ES512",
  EdDSA: "EdDSA",
  Ps256: "PS256",
  Ps384: "PS384",
  Ps512: "PS512",
  Rs256: "RS256",
  Rs384: "RS384",
  Rs512: "RS512",
} as const;
export type ListKmsIssuersAlgorithm = ClosedEnum<
  typeof ListKmsIssuersAlgorithm
>;

export const ListKmsIssuersOrigin = {
  External: "external",
  Vercel: "vercel",
} as const;
export type ListKmsIssuersOrigin = ClosedEnum<typeof ListKmsIssuersOrigin>;

export const ListKmsIssuersStatus = {
  Active: "active",
  Pending: "pending",
  Revoking: "revoking",
} as const;
export type ListKmsIssuersStatus = ClosedEnum<typeof ListKmsIssuersStatus>;

export type ListKmsIssuersPublicKey = {
  kty?: string | undefined;
  kid?: string | undefined;
  alg?: string | undefined;
  use?: string | undefined;
  keyOps?: Array<string> | undefined;
  /**
   * The X.509 certificate chain (RFC 7517 §4.7). Each entry is the base64 DER (not base64url) of a certificate. For keys minted with a stored certificate this holds the single self-signed cert as `[x5c]`.
   */
  x5c?: Array<string> | undefined;
  /**
   * The base64url SHA-256 thumbprint of the DER certificate in `x5c[0]` (RFC 7517 §4.9).
   */
  x5tNumberS256?: string | undefined;
};

export type ListKmsIssuersSigningKeys = {
  /**
   * The server-minted, unique record identifier. Use this to address the key on the activate / certificate endpoints.
   */
  keyId: string;
  /**
   * The caller-supplied key id (imported keys only), used as the JWT/JWKS `kid`. Not unique across an issuer's keys; omitted for generated keys.
   */
  importKeyId?: string | undefined;
  issuerId: string;
  algorithm: string;
  status: ListKmsIssuersStatus;
  publicKey?: ListKmsIssuersPublicKey | undefined;
  publicKeyFingerprint?: string | undefined;
  /**
   * The public key in SPKI PEM form, ready to render. Present whenever the key has public key material. Derived from `publicKey`; the embedded certificate members (`x5c`/`x5t#S256`) do not affect it.
   */
  publicKeyPem?: string | undefined;
  /**
   * The stored X.509 certificate (from `publicKey.x5c[0]`) in PEM form, ready to render. Present only for keys created with a stored certificate; omitted for keys created before certificates were stored.
   */
  certificatePem?: string | undefined;
  createdAt: string;
  updatedAt: string;
  revokeAt?: string | undefined;
  activateAt?: string | undefined;
  /**
   * When the key became the active signer. Present for active and revoking keys (and absent for pending keys and rows predating this field).
   */
  activatedAt?: string | undefined;
};

export type ListKmsIssuersPolicies2 = {
  kind: "connex-grant";
  clientId: string;
  tokenClaims?: { [k: string]: any } | undefined;
  createdAt: string;
  updatedAt: string;
};

export type ListKmsIssuersPolicies1 = {
  kind: "project-grant";
  teamId: string;
  projectId: string;
  /**
   * Environments whose OIDC tokens this grant authorizes. Each entry is either a system environment slug (`production`, `preview`, `development`) or a custom environment ID (prefixed `env_`). Custom environments are matched against the token's `custom_environment_id` claim (the stable ID); system environments against its `environment` claim.
   */
  environments: Array<string>;
  tokenClaims?: { [k: string]: any } | undefined;
  createdAt: string;
  updatedAt: string;
};

export type ListKmsIssuersPolicies =
  | ListKmsIssuersPolicies1
  | ListKmsIssuersPolicies2;

export type Issuers = {
  id: string;
  ownerId: string;
  name: string;
  algorithm: ListKmsIssuersAlgorithm;
  origin: ListKmsIssuersOrigin;
  managedBy?: string | undefined;
  claimsSchema?: { [k: string]: any } | undefined;
  createdAt: string;
  updatedAt: string;
  signingKeys: Array<ListKmsIssuersSigningKeys>;
  policies: Array<ListKmsIssuersPolicies1 | ListKmsIssuersPolicies2>;
};

export type ListKmsIssuersPagination = {
  count: number;
  next: string | null;
};

export type ListKmsIssuersResponseBody = {
  issuers: Array<Issuers>;
  pagination: ListKmsIssuersPagination;
};

/** @internal */
export type ListKmsIssuersRequest$Outbound = {
  limit?: number | undefined;
  next?: string | undefined;
  teamId?: string | undefined;
  slug?: string | undefined;
};

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

export function listKmsIssuersRequestToJSON(
  listKmsIssuersRequest: ListKmsIssuersRequest,
): string {
  return JSON.stringify(
    ListKmsIssuersRequest$outboundSchema.parse(listKmsIssuersRequest),
  );
}

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

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

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

/** @internal */
export const ListKmsIssuersPublicKey$inboundSchema: z.ZodType<
  ListKmsIssuersPublicKey,
  z.ZodTypeDef,
  unknown
> = z.object({
  kty: types.optional(types.string()),
  kid: types.optional(types.string()),
  alg: types.optional(types.string()),
  use: types.optional(types.string()),
  key_ops: types.optional(z.array(types.string())),
  x5c: types.optional(z.array(types.string())),
  "x5t#S256": types.optional(types.string()),
}).transform((v) => {
  return remap$(v, {
    "key_ops": "keyOps",
    "x5t#S256": "x5tNumberS256",
  });
});

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

/** @internal */
export const ListKmsIssuersSigningKeys$inboundSchema: z.ZodType<
  ListKmsIssuersSigningKeys,
  z.ZodTypeDef,
  unknown
> = z.object({
  keyId: types.string(),
  importKeyId: types.optional(types.string()),
  issuerId: types.string(),
  algorithm: types.string(),
  status: ListKmsIssuersStatus$inboundSchema,
  publicKey: types.optional(
    z.lazy(() => ListKmsIssuersPublicKey$inboundSchema),
  ),
  publicKeyFingerprint: types.optional(types.string()),
  publicKeyPem: types.optional(types.string()),
  certificatePem: types.optional(types.string()),
  createdAt: types.string(),
  updatedAt: types.string(),
  revokeAt: types.optional(types.string()),
  activateAt: types.optional(types.string()),
  activatedAt: types.optional(types.string()),
});

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

/** @internal */
export const ListKmsIssuersPolicies2$inboundSchema: z.ZodType<
  ListKmsIssuersPolicies2,
  z.ZodTypeDef,
  unknown
> = z.object({
  kind: types.literal("connex-grant"),
  clientId: types.string(),
  tokenClaims: types.optional(z.record(z.any())),
  createdAt: types.string(),
  updatedAt: types.string(),
});

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

/** @internal */
export const ListKmsIssuersPolicies1$inboundSchema: z.ZodType<
  ListKmsIssuersPolicies1,
  z.ZodTypeDef,
  unknown
> = z.object({
  kind: types.literal("project-grant"),
  teamId: types.string(),
  projectId: types.string(),
  environments: z.array(types.string()),
  tokenClaims: types.optional(z.record(z.any())),
  createdAt: types.string(),
  updatedAt: types.string(),
});

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

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

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

/** @internal */
export const Issuers$inboundSchema: z.ZodType<Issuers, z.ZodTypeDef, unknown> =
  z.object({
    id: types.string(),
    ownerId: types.string(),
    name: types.string(),
    algorithm: ListKmsIssuersAlgorithm$inboundSchema,
    origin: ListKmsIssuersOrigin$inboundSchema,
    managedBy: types.optional(types.string()),
    claimsSchema: types.optional(z.record(z.any())),
    createdAt: types.string(),
    updatedAt: types.string(),
    signingKeys: z.array(z.lazy(() => ListKmsIssuersSigningKeys$inboundSchema)),
    policies: z.array(
      z.union([
        z.lazy(() => ListKmsIssuersPolicies1$inboundSchema),
        z.lazy(() => ListKmsIssuersPolicies2$inboundSchema),
      ]),
    ),
  });

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

/** @internal */
export const ListKmsIssuersPagination$inboundSchema: z.ZodType<
  ListKmsIssuersPagination,
  z.ZodTypeDef,
  unknown
> = z.object({
  count: types.number(),
  next: types.nullable(types.string()),
});

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

/** @internal */
export const ListKmsIssuersResponseBody$inboundSchema: z.ZodType<
  ListKmsIssuersResponseBody,
  z.ZodTypeDef,
  unknown
> = z.object({
  issuers: z.array(z.lazy(() => Issuers$inboundSchema)),
  pagination: z.lazy(() => ListKmsIssuersPagination$inboundSchema),
});

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