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

export const MINIMUM_DEPOSIT_DISCRIMINATOR = new Uint8Array([
  218, 139, 120, 202, 3, 12, 233, 65,
]);

export function getMinimumDepositDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    MINIMUM_DEPOSIT_DISCRIMINATOR,
  );
}

export type MinimumDeposit = {
  discriminator: ReadonlyUint8Array;
  amount: bigint;
  bump: number;
};

export type MinimumDepositArgs = { amount: number | bigint; bump: number };

/** Gets the encoder for {@link MinimumDepositArgs} account data. */
export function getMinimumDepositEncoder(): FixedSizeEncoder<MinimumDepositArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["amount", getU64Encoder()],
      ["bump", getU8Encoder()],
    ]),
    (value) => ({ ...value, discriminator: MINIMUM_DEPOSIT_DISCRIMINATOR }),
  );
}

/** Gets the decoder for {@link MinimumDeposit} account data. */
export function getMinimumDepositDecoder(): FixedSizeDecoder<MinimumDeposit> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["amount", getU64Decoder()],
    ["bump", getU8Decoder()],
  ]);
}

/** Gets the codec for {@link MinimumDeposit} account data. */
export function getMinimumDepositCodec(): FixedSizeCodec<
  MinimumDepositArgs,
  MinimumDeposit
> {
  return combineCodec(getMinimumDepositEncoder(), getMinimumDepositDecoder());
}

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

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

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

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

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

export function getMinimumDepositSize(): number {
  return 17;
}
