/**
 * This code was AUTOGENERATED using the codama library.
 * Please DO NOT EDIT THIS FILE, instead use visitors
 * to add features, then rerun codama to update it.
 *
 * @see https://github.com/codama-idl/codama
 */

import {
  assertAccountExists,
  assertAccountsExist,
  combineCodec,
  decodeAccount,
  fetchEncodedAccount,
  fetchEncodedAccounts,
  getAddressDecoder,
  getAddressEncoder,
  getLamportsDecoder,
  getLamportsEncoder,
  getStructDecoder,
  getStructEncoder,
  getU64Decoder,
  getU64Encoder,
  type Account,
  type Address,
  type EncodedAccount,
  type FetchAccountConfig,
  type FetchAccountsConfig,
  type FixedSizeCodec,
  type FixedSizeDecoder,
  type FixedSizeEncoder,
  type Lamports,
  type MaybeAccount,
  type MaybeEncodedAccount,
} from '@solana/kit';
import {
  getNonceStateDecoder,
  getNonceStateEncoder,
  getNonceVersionDecoder,
  getNonceVersionEncoder,
  type NonceState,
  type NonceStateArgs,
  type NonceVersion,
  type NonceVersionArgs,
} from '../types';

export type Nonce = {
  version: NonceVersion;
  state: NonceState;
  authority: Address;
  blockhash: Address;
  lamportsPerSignature: Lamports;
};

export type NonceArgs = {
  version: NonceVersionArgs;
  state: NonceStateArgs;
  authority: Address;
  blockhash: Address;
  lamportsPerSignature: Lamports;
};

export function getNonceEncoder(): FixedSizeEncoder<NonceArgs> {
  return getStructEncoder([
    ['version', getNonceVersionEncoder()],
    ['state', getNonceStateEncoder()],
    ['authority', getAddressEncoder()],
    ['blockhash', getAddressEncoder()],
    ['lamportsPerSignature', getLamportsEncoder(getU64Encoder())],
  ]) as FixedSizeEncoder<NonceArgs>;
}

export function getNonceDecoder(): FixedSizeDecoder<Nonce> {
  return getStructDecoder([
    ['version', getNonceVersionDecoder()],
    ['state', getNonceStateDecoder()],
    ['authority', getAddressDecoder()],
    ['blockhash', getAddressDecoder()],
    ['lamportsPerSignature', getLamportsDecoder(getU64Decoder())],
  ]) as FixedSizeDecoder<Nonce>;
}

export function getNonceCodec(): FixedSizeCodec<NonceArgs, Nonce> {
  return combineCodec(getNonceEncoder(), getNonceDecoder()) as FixedSizeCodec<
    NonceArgs,
    Nonce
  >;
}

export function decodeNonce<TAddress extends string = string>(
  encodedAccount: EncodedAccount<TAddress>
): Account<Nonce, TAddress>;
export function decodeNonce<TAddress extends string = string>(
  encodedAccount: MaybeEncodedAccount<TAddress>
): MaybeAccount<Nonce, TAddress>;
export function decodeNonce<TAddress extends string = string>(
  encodedAccount: EncodedAccount<TAddress> | MaybeEncodedAccount<TAddress>
): Account<Nonce, TAddress> | MaybeAccount<Nonce, TAddress> {
  return decodeAccount(
    encodedAccount as MaybeEncodedAccount<TAddress>,
    getNonceDecoder()
  );
}

export async function fetchNonce<TAddress extends string = string>(
  rpc: Parameters<typeof fetchEncodedAccount>[0],
  address: Address<TAddress>,
  config?: FetchAccountConfig
): Promise<Account<Nonce, TAddress>> {
  const maybeAccount = await fetchMaybeNonce(rpc, address, config);
  assertAccountExists(maybeAccount);
  return maybeAccount;
}

export async function fetchMaybeNonce<TAddress extends string = string>(
  rpc: Parameters<typeof fetchEncodedAccount>[0],
  address: Address<TAddress>,
  config?: FetchAccountConfig
): Promise<MaybeAccount<Nonce, TAddress>> {
  const maybeAccount = await fetchEncodedAccount(rpc, address, config);
  return decodeNonce(maybeAccount);
}

export async function fetchAllNonce(
  rpc: Parameters<typeof fetchEncodedAccounts>[0],
  addresses: Array<Address>,
  config?: FetchAccountsConfig
): Promise<Account<Nonce>[]> {
  const maybeAccounts = await fetchAllMaybeNonce(rpc, addresses, config);
  assertAccountsExist(maybeAccounts);
  return maybeAccounts;
}

export async function fetchAllMaybeNonce(
  rpc: Parameters<typeof fetchEncodedAccounts>[0],
  addresses: Array<Address>,
  config?: FetchAccountsConfig
): Promise<MaybeAccount<Nonce>[]> {
  const maybeAccounts = await fetchEncodedAccounts(rpc, addresses, config);
  return maybeAccounts.map((maybeAccount) => decodeNonce(maybeAccount));
}

export function getNonceSize(): number {
  return 80;
}
