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

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

export const GetTokenFromJWTGrantServerList = [
  /**
   * For the developer environment, the URI is https://account-d.docusign.com/oauth/token
   */
  "https://account-d.docusign.com",
  /**
   * For the production environment, the URI is https://account.docusign.com/oauth/token
   */
  "https://account.docusign.com",
] as const;

/**
 * The grant type.
 */
export const GetTokenFromJWTGrantGrantType = {
  UrnIetfParamsOauthGrantTypeJwtBearer:
    "urn:ietf:params:oauth:grant-type:jwt-bearer",
} as const;
/**
 * The grant type.
 */
export type GetTokenFromJWTGrantGrantType = ClosedEnum<
  typeof GetTokenFromJWTGrantGrantType
>;

/**
 * JSON Web Token (JWT) Grant is an OAuth 2.0 flow that is used to grant an access token to service integrations
 */
export type JWTGrant = {
  /**
   * The grant type.
   */
  grantType?: GetTokenFromJWTGrantGrantType | undefined;
  /**
   * Your JWT
   */
  assertion: string;
};

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

/** @internal */
export const GetTokenFromJWTGrantGrantType$outboundSchema: z.ZodNativeEnum<
  typeof GetTokenFromJWTGrantGrantType
> = GetTokenFromJWTGrantGrantType$inboundSchema;

/**
 * @internal
 * @deprecated This namespace will be removed in future versions. Use schemas and types that are exported directly from this module.
 */
export namespace GetTokenFromJWTGrantGrantType$ {
  /** @deprecated use `GetTokenFromJWTGrantGrantType$inboundSchema` instead. */
  export const inboundSchema = GetTokenFromJWTGrantGrantType$inboundSchema;
  /** @deprecated use `GetTokenFromJWTGrantGrantType$outboundSchema` instead. */
  export const outboundSchema = GetTokenFromJWTGrantGrantType$outboundSchema;
}

/** @internal */
export const JWTGrant$inboundSchema: z.ZodType<
  JWTGrant,
  z.ZodTypeDef,
  unknown
> = z.object({
  grant_type: GetTokenFromJWTGrantGrantType$inboundSchema.default(
    "urn:ietf:params:oauth:grant-type:jwt-bearer",
  ),
  assertion: z.string(),
}).transform((v) => {
  return remap$(v, {
    "grant_type": "grantType",
  });
});

/** @internal */
export type JWTGrant$Outbound = {
  grant_type: string;
  assertion: string;
};

/** @internal */
export const JWTGrant$outboundSchema: z.ZodType<
  JWTGrant$Outbound,
  z.ZodTypeDef,
  JWTGrant
> = z.object({
  grantType: GetTokenFromJWTGrantGrantType$outboundSchema.default(
    "urn:ietf:params:oauth:grant-type:jwt-bearer",
  ),
  assertion: z.string(),
}).transform((v) => {
  return remap$(v, {
    grantType: "grant_type",
  });
});

/**
 * @internal
 * @deprecated This namespace will be removed in future versions. Use schemas and types that are exported directly from this module.
 */
export namespace JWTGrant$ {
  /** @deprecated use `JWTGrant$inboundSchema` instead. */
  export const inboundSchema = JWTGrant$inboundSchema;
  /** @deprecated use `JWTGrant$outboundSchema` instead. */
  export const outboundSchema = JWTGrant$outboundSchema;
  /** @deprecated use `JWTGrant$Outbound` instead. */
  export type Outbound = JWTGrant$Outbound;
}

export function jwtGrantToJSON(jwtGrant: JWTGrant): string {
  return JSON.stringify(JWTGrant$outboundSchema.parse(jwtGrant));
}

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