/**
 * 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,
  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_NONCE_DISCRIMINATOR = new Uint8Array([
  212, 222, 157, 252, 130, 71, 179, 238,
]);

export function getUsedNonceDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(USED_NONCE_DISCRIMINATOR);
}

export type UsedNonce = {
  discriminator: ReadonlyUint8Array;
  quoteDeadline: bigint;
};

export type UsedNonceArgs = { quoteDeadline: number | bigint };

/** Gets the encoder for {@link UsedNonceArgs} account data. */
export function getUsedNonceEncoder(): FixedSizeEncoder<UsedNonceArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["quoteDeadline", getU64Encoder()],
    ]),
    (value) => ({ ...value, discriminator: USED_NONCE_DISCRIMINATOR }),
  );
}

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

/** Gets the codec for {@link UsedNonce} account data. */
export function getUsedNonceCodec(): FixedSizeCodec<UsedNonceArgs, UsedNonce> {
  return combineCodec(getUsedNonceEncoder(), getUsedNonceDecoder());
}

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

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

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

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

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

export function getUsedNonceSize(): number {
  return 16;
}
