/**
 * 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,
  getArrayDecoder,
  getArrayEncoder,
  getStructDecoder,
  getStructEncoder,
  getU64Decoder,
  getU64Encoder,
  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 Instance = {
  discriminator: number;
  bump: number;
  version: number;
  instanceSeed: Address;
  admin: Address;
  withdrawalTransactionsRoot: Array<number>;
  currentTreeIndex: bigint;
};

export type InstanceArgs = {
  discriminator: number;
  bump: number;
  version: number;
  instanceSeed: Address;
  admin: Address;
  withdrawalTransactionsRoot: Array<number>;
  currentTreeIndex: number | bigint;
};

/** Gets the encoder for {@link InstanceArgs} account data. */
export function getInstanceEncoder(): FixedSizeEncoder<InstanceArgs> {
  return getStructEncoder([
    ["discriminator", getU8Encoder()],
    ["bump", getU8Encoder()],
    ["version", getU8Encoder()],
    ["instanceSeed", getAddressEncoder()],
    ["admin", getAddressEncoder()],
    [
      "withdrawalTransactionsRoot",
      getArrayEncoder(getU8Encoder(), { size: 32 }),
    ],
    ["currentTreeIndex", getU64Encoder()],
  ]);
}

/** Gets the decoder for {@link Instance} account data. */
export function getInstanceDecoder(): FixedSizeDecoder<Instance> {
  return getStructDecoder([
    ["discriminator", getU8Decoder()],
    ["bump", getU8Decoder()],
    ["version", getU8Decoder()],
    ["instanceSeed", getAddressDecoder()],
    ["admin", getAddressDecoder()],
    [
      "withdrawalTransactionsRoot",
      getArrayDecoder(getU8Decoder(), { size: 32 }),
    ],
    ["currentTreeIndex", getU64Decoder()],
  ]);
}

/** Gets the codec for {@link Instance} account data. */
export function getInstanceCodec(): FixedSizeCodec<InstanceArgs, Instance> {
  return combineCodec(getInstanceEncoder(), getInstanceDecoder());
}

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

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

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

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

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