/*
 * 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 { Result as SafeParseResult } from "../types/fp.js";
import * as types from "../types/primitives.js";
import { SDKValidationError } from "./sdkvalidationerror.js";

export type SignKmsMessageRequestBody = {
  /**
   * Base64-encoded message to be signed.
   */
  message: string;
};

export type SignKmsMessageRequest = {
  /**
   * The ID of the issuer.
   */
  issuerId: string;
  requestBody?: SignKmsMessageRequestBody | undefined;
};

/**
 * "jwk" (JSON Web Key) Header Parameter
 */
export type Jwk = {
  /**
   * RSA JWK "n" (Modulus) Parameter
   */
  n?: string | undefined;
  /**
   * RSA JWK "e" (Exponent) Parameter
   */
  e?: string | undefined;
  /**
   * JWK "kty" (Key Type) Parameter
   */
  kty?: string | undefined;
  /**
   * - EC JWK "crv" (Curve) Parameter - OKP JWK "crv" (The Subtype of Key Pair) Parameter
   */
  crv?: string | undefined;
  /**
   * - EC JWK "x" (X Coordinate) Parameter - OKP JWK "x" (The public key) Parameter
   */
  x?: string | undefined;
  /**
   * EC JWK "y" (Y Coordinate) Parameter
   */
  y?: string | undefined;
  /**
   * JWK "alg" (Algorithm) Parameter
   */
  alg?: string | undefined;
  /**
   * AKP JWK "pub" (Public Key) Parameter
   */
  pub?: string | undefined;
};

/**
 * The "header" member MUST be present and contain the value JWS Unprotected Header when the JWS Unprotected Header value is non- empty; otherwise, it MUST be absent. This value is represented as an unencoded JSON object, rather than as a string. These Header Parameter values are not integrity protected.
 */
export type Header = {
  /**
   * JWS "alg" (Algorithm) Header Parameter
   */
  alg?: string | undefined;
  /**
   * This JWS Extension Header Parameter modifies the JWS Payload representation and the JWS Signing Input computation as per {@link https://www.rfc-editor.org/rfc/rfc7797 RFC7797}.
   */
  b64?: boolean | undefined;
  /**
   * JWS "crit" (Critical) Header Parameter
   */
  crit?: Array<string> | undefined;
  /**
   * "kid" (Key ID) Header Parameter
   */
  kid?: string | undefined;
  /**
   * "x5t" (X.509 Certificate SHA-1 Thumbprint) Header Parameter
   */
  x5t?: string | undefined;
  /**
   * "x5c" (X.509 Certificate Chain) Header Parameter
   */
  x5c?: Array<string> | undefined;
  /**
   * "x5u" (X.509 URL) Header Parameter
   */
  x5u?: string | undefined;
  /**
   * "jku" (JWK Set URL) Header Parameter
   */
  jku?: string | undefined;
  /**
   * "jwk" (JSON Web Key) Header Parameter
   */
  jwk?: Jwk | undefined;
  /**
   * "typ" (Type) Header Parameter
   */
  typ?: string | undefined;
  /**
   * "cty" (Content Type) Header Parameter
   */
  cty?: string | undefined;
};

/**
 * Flattened JWS JSON Serialization Syntax token. Payload is returned as an empty string when JWS Unencoded Payload ({@link https://www.rfc-editor.org/rfc/rfc7797 RFC7797}) is used.
 */
export type Signature = {
  payload: string;
  signature: string;
  /**
   * The "header" member MUST be present and contain the value JWS Unprotected Header when the JWS Unprotected Header value is non- empty; otherwise, it MUST be absent. This value is represented as an unencoded JSON object, rather than as a string. These Header Parameter values are not integrity protected.
   */
  header?: Header | undefined;
  /**
   * The "protected" member MUST be present and contain the value BASE64URL(UTF8(JWS Protected Header)) when the JWS Protected Header value is non-empty; otherwise, it MUST be absent. These Header Parameter values are integrity protected.
   */
  protected?: string | undefined;
};

export type SignKmsMessageResponseBody = {
  /**
   * Flattened JWS JSON Serialization Syntax token. Payload is returned as an empty string when JWS Unencoded Payload ({@link https://www.rfc-editor.org/rfc/rfc7797 RFC7797}) is used.
   */
  signature: Signature;
};

/** @internal */
export type SignKmsMessageRequestBody$Outbound = {
  message: string;
};

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

export function signKmsMessageRequestBodyToJSON(
  signKmsMessageRequestBody: SignKmsMessageRequestBody,
): string {
  return JSON.stringify(
    SignKmsMessageRequestBody$outboundSchema.parse(signKmsMessageRequestBody),
  );
}

/** @internal */
export type SignKmsMessageRequest$Outbound = {
  issuerId: string;
  RequestBody?: SignKmsMessageRequestBody$Outbound | undefined;
};

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

export function signKmsMessageRequestToJSON(
  signKmsMessageRequest: SignKmsMessageRequest,
): string {
  return JSON.stringify(
    SignKmsMessageRequest$outboundSchema.parse(signKmsMessageRequest),
  );
}

/** @internal */
export const Jwk$inboundSchema: z.ZodType<Jwk, z.ZodTypeDef, unknown> = z
  .object({
    n: types.optional(types.string()),
    e: types.optional(types.string()),
    kty: types.optional(types.string()),
    crv: types.optional(types.string()),
    x: types.optional(types.string()),
    y: types.optional(types.string()),
    alg: types.optional(types.string()),
    pub: types.optional(types.string()),
  });

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

/** @internal */
export const Header$inboundSchema: z.ZodType<Header, z.ZodTypeDef, unknown> = z
  .object({
    alg: types.optional(types.string()),
    b64: types.optional(types.boolean()),
    crit: types.optional(z.array(types.string())),
    kid: types.optional(types.string()),
    x5t: types.optional(types.string()),
    x5c: types.optional(z.array(types.string())),
    x5u: types.optional(types.string()),
    jku: types.optional(types.string()),
    jwk: types.optional(z.lazy(() => Jwk$inboundSchema)),
    typ: types.optional(types.string()),
    cty: types.optional(types.string()),
  });

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

/** @internal */
export const Signature$inboundSchema: z.ZodType<
  Signature,
  z.ZodTypeDef,
  unknown
> = z.object({
  payload: types.string(),
  signature: types.string(),
  header: types.optional(z.lazy(() => Header$inboundSchema)),
  protected: types.optional(types.string()),
});

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

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

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