/**
 * 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,
  getStructDecoder,
  getStructEncoder,
  getU8Decoder,
  getU8Encoder,
  type Account,
  type Address,
  type EncodedAccount,
  type FetchAccountConfig,
  type FetchAccountsConfig,
  type FixedSizeCodec,
  type FixedSizeDecoder,
  type FixedSizeEncoder,
  type MaybeAccount,
  type MaybeEncodedAccount,
} from "@solana/kit";

export type AllowedMint = { discriminator: number; bump: number };

export type AllowedMintArgs = AllowedMint;

/** Gets the encoder for {@link AllowedMintArgs} account data. */
export function getAllowedMintEncoder(): FixedSizeEncoder<AllowedMintArgs> {
  return getStructEncoder([
    ["discriminator", getU8Encoder()],
    ["bump", getU8Encoder()],
  ]);
}

/** Gets the decoder for {@link AllowedMint} account data. */
export function getAllowedMintDecoder(): FixedSizeDecoder<AllowedMint> {
  return getStructDecoder([
    ["discriminator", getU8Decoder()],
    ["bump", getU8Decoder()],
  ]);
}

/** Gets the codec for {@link AllowedMint} account data. */
export function getAllowedMintCodec(): FixedSizeCodec<
  AllowedMintArgs,
  AllowedMint
> {
  return combineCodec(getAllowedMintEncoder(), getAllowedMintDecoder());
}

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

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

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

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

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