/**
 * 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,
  getArrayDecoder,
  getArrayEncoder,
  getBytesDecoder,
  getBytesEncoder,
  getStructDecoder,
  getStructEncoder,
  getU32Decoder,
  getU32Encoder,
  getU64Decoder,
  getU64Encoder,
  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 USED_NONCES_DISCRIMINATOR = new Uint8Array([
  60, 112, 18, 72, 138, 181, 100, 138,
]);

export function getUsedNoncesDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(USED_NONCES_DISCRIMINATOR);
}

export type UsedNonces = {
  discriminator: ReadonlyUint8Array;
  remoteDomain: number;
  firstNonce: bigint;
  usedNonces: Array<bigint>;
};

export type UsedNoncesArgs = {
  remoteDomain: number;
  firstNonce: number | bigint;
  usedNonces: Array<number | bigint>;
};

/** Gets the encoder for {@link UsedNoncesArgs} account data. */
export function getUsedNoncesEncoder(): FixedSizeEncoder<UsedNoncesArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["remoteDomain", getU32Encoder()],
      ["firstNonce", getU64Encoder()],
      ["usedNonces", getArrayEncoder(getU64Encoder(), { size: 100 })],
    ]),
    (value) => ({ ...value, discriminator: USED_NONCES_DISCRIMINATOR }),
  );
}

/** Gets the decoder for {@link UsedNonces} account data. */
export function getUsedNoncesDecoder(): FixedSizeDecoder<UsedNonces> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["remoteDomain", getU32Decoder()],
    ["firstNonce", getU64Decoder()],
    ["usedNonces", getArrayDecoder(getU64Decoder(), { size: 100 })],
  ]);
}

/** Gets the codec for {@link UsedNonces} account data. */
export function getUsedNoncesCodec(): FixedSizeCodec<
  UsedNoncesArgs,
  UsedNonces
> {
  return combineCodec(getUsedNoncesEncoder(), getUsedNoncesDecoder());
}

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

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

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

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

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

export function getUsedNoncesSize(): number {
  return 820;
}
