/*
 * 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 "../errors/sdkvalidationerror.js";

export const GetTokenFromRefreshTokenServerList = [
  /**
   * 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;

export type GetTokenFromRefreshTokenSecurity = {
  clientId?: string | undefined;
  secretKey?: string | undefined;
};

/**
 * The grant type. This value must be set to "refresh_token".
 */
export const GetTokenFromRefreshTokenGrantType = {
  RefreshToken: "refresh_token",
} as const;
/**
 * The grant type. This value must be set to "refresh_token".
 */
export type GetTokenFromRefreshTokenGrantType = ClosedEnum<
  typeof GetTokenFromRefreshTokenGrantType
>;

/**
 * This grant optionally uses Proof Key for Code Exchange (PKCE) to authenticate securely without requiring a client secret.
 */
export type AuthorizationCodeGrant = {
  /**
   * The grant type. This value must be set to "refresh_token".
   */
  grantType?: GetTokenFromRefreshTokenGrantType | undefined;
  /**
   * The refresh token supplied to the callback.
   */
  refreshToken: string;
  /**
   * The client ID.
   */
  clientId?: string | undefined;
};

/**
 * Successful response.
 */
export type GetTokenFromRefreshTokenResponse = {
  /**
   * The value of the access token. This value will be added to the Authorization header of all Docusign API calls.
   */
  accessToken: string;
  /**
   * The type of token. For access tokens, the value of this will be Bearer.
   */
  tokenType: string;
  /**
   * The refresh token.
   */
  refreshToken: string;
  /**
   * The number of seconds until the access token expires.
   */
  expiresIn: number;
};

/** @internal */
export type GetTokenFromRefreshTokenSecurity$Outbound = {
  clientId?: string | undefined;
  secretKey?: string | undefined;
};

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

export function getTokenFromRefreshTokenSecurityToJSON(
  getTokenFromRefreshTokenSecurity: GetTokenFromRefreshTokenSecurity,
): string {
  return JSON.stringify(
    GetTokenFromRefreshTokenSecurity$outboundSchema.parse(
      getTokenFromRefreshTokenSecurity,
    ),
  );
}

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

/** @internal */
export type AuthorizationCodeGrant$Outbound = {
  grant_type: string;
  refresh_token: string;
  client_id?: string | undefined;
};

/** @internal */
export const AuthorizationCodeGrant$outboundSchema: z.ZodType<
  AuthorizationCodeGrant$Outbound,
  z.ZodTypeDef,
  AuthorizationCodeGrant
> = z.object({
  grantType: GetTokenFromRefreshTokenGrantType$outboundSchema.default(
    "refresh_token",
  ),
  refreshToken: z.string(),
  clientId: z.string().optional(),
}).transform((v) => {
  return remap$(v, {
    grantType: "grant_type",
    refreshToken: "refresh_token",
    clientId: "client_id",
  });
});

export function authorizationCodeGrantToJSON(
  authorizationCodeGrant: AuthorizationCodeGrant,
): string {
  return JSON.stringify(
    AuthorizationCodeGrant$outboundSchema.parse(authorizationCodeGrant),
  );
}

/** @internal */
export const GetTokenFromRefreshTokenResponse$inboundSchema: z.ZodType<
  GetTokenFromRefreshTokenResponse,
  z.ZodTypeDef,
  unknown
> = z.object({
  access_token: types.string(),
  token_type: types.string(),
  refresh_token: types.string(),
  expires_in: types.number(),
}).transform((v) => {
  return remap$(v, {
    "access_token": "accessToken",
    "token_type": "tokenType",
    "refresh_token": "refreshToken",
    "expires_in": "expiresIn",
  });
});

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