/**
 * 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 {
  addDecoderSizePrefix,
  addEncoderSizePrefix,
  assertAccountExists,
  assertAccountsExist,
  combineCodec,
  decodeAccount,
  fetchEncodedAccount,
  fetchEncodedAccounts,
  fixDecoderSize,
  fixEncoderSize,
  getBytesDecoder,
  getBytesEncoder,
  getStructDecoder,
  getStructEncoder,
  getU32Decoder,
  getU32Encoder,
  transformEncoder,
  type Account,
  type Address,
  type Codec,
  type Decoder,
  type EncodedAccount,
  type Encoder,
  type FetchAccountConfig,
  type FetchAccountsConfig,
  type MaybeAccount,
  type MaybeEncodedAccount,
  type ReadonlyUint8Array,
} from "@solana/kit";

export const ROOT_BUNDLE_DISCRIMINATOR = new Uint8Array([
  66, 221, 214, 231, 25, 222, 184, 219,
]);

export function getRootBundleDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(ROOT_BUNDLE_DISCRIMINATOR);
}

export type RootBundle = {
  discriminator: ReadonlyUint8Array;
  relayerRefundRoot: ReadonlyUint8Array;
  slowRelayRoot: ReadonlyUint8Array;
  claimedBitmap: ReadonlyUint8Array;
};

export type RootBundleArgs = {
  relayerRefundRoot: ReadonlyUint8Array;
  slowRelayRoot: ReadonlyUint8Array;
  claimedBitmap: ReadonlyUint8Array;
};

/** Gets the encoder for {@link RootBundleArgs} account data. */
export function getRootBundleEncoder(): Encoder<RootBundleArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["relayerRefundRoot", fixEncoderSize(getBytesEncoder(), 32)],
      ["slowRelayRoot", fixEncoderSize(getBytesEncoder(), 32)],
      [
        "claimedBitmap",
        addEncoderSizePrefix(getBytesEncoder(), getU32Encoder()),
      ],
    ]),
    (value) => ({ ...value, discriminator: ROOT_BUNDLE_DISCRIMINATOR }),
  );
}

/** Gets the decoder for {@link RootBundle} account data. */
export function getRootBundleDecoder(): Decoder<RootBundle> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["relayerRefundRoot", fixDecoderSize(getBytesDecoder(), 32)],
    ["slowRelayRoot", fixDecoderSize(getBytesDecoder(), 32)],
    ["claimedBitmap", addDecoderSizePrefix(getBytesDecoder(), getU32Decoder())],
  ]);
}

/** Gets the codec for {@link RootBundle} account data. */
export function getRootBundleCodec(): Codec<RootBundleArgs, RootBundle> {
  return combineCodec(getRootBundleEncoder(), getRootBundleDecoder());
}

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

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

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

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

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