/**
 * 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,
  getAddressDecoder,
  getAddressEncoder,
  getBytesDecoder,
  getBytesEncoder,
  getStructDecoder,
  getStructEncoder,
  getU128Decoder,
  getU128Encoder,
  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 LOCAL_TOKEN_DISCRIMINATOR = new Uint8Array([
  159, 131, 58, 170, 193, 84, 128, 182,
]);

export function getLocalTokenDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(LOCAL_TOKEN_DISCRIMINATOR);
}

export type LocalToken = {
  discriminator: ReadonlyUint8Array;
  custody: Address;
  mint: Address;
  burnLimitPerMessage: bigint;
  messagesSent: bigint;
  messagesReceived: bigint;
  amountSent: bigint;
  amountReceived: bigint;
  bump: number;
  custodyBump: number;
};

export type LocalTokenArgs = {
  custody: Address;
  mint: Address;
  burnLimitPerMessage: number | bigint;
  messagesSent: number | bigint;
  messagesReceived: number | bigint;
  amountSent: number | bigint;
  amountReceived: number | bigint;
  bump: number;
  custodyBump: number;
};

/** Gets the encoder for {@link LocalTokenArgs} account data. */
export function getLocalTokenEncoder(): FixedSizeEncoder<LocalTokenArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["custody", getAddressEncoder()],
      ["mint", getAddressEncoder()],
      ["burnLimitPerMessage", getU64Encoder()],
      ["messagesSent", getU64Encoder()],
      ["messagesReceived", getU64Encoder()],
      ["amountSent", getU128Encoder()],
      ["amountReceived", getU128Encoder()],
      ["bump", getU8Encoder()],
      ["custodyBump", getU8Encoder()],
    ]),
    (value) => ({ ...value, discriminator: LOCAL_TOKEN_DISCRIMINATOR }),
  );
}

/** Gets the decoder for {@link LocalToken} account data. */
export function getLocalTokenDecoder(): FixedSizeDecoder<LocalToken> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["custody", getAddressDecoder()],
    ["mint", getAddressDecoder()],
    ["burnLimitPerMessage", getU64Decoder()],
    ["messagesSent", getU64Decoder()],
    ["messagesReceived", getU64Decoder()],
    ["amountSent", getU128Decoder()],
    ["amountReceived", getU128Decoder()],
    ["bump", getU8Decoder()],
    ["custodyBump", getU8Decoder()],
  ]);
}

/** Gets the codec for {@link LocalToken} account data. */
export function getLocalTokenCodec(): FixedSizeCodec<
  LocalTokenArgs,
  LocalToken
> {
  return combineCodec(getLocalTokenEncoder(), getLocalTokenDecoder());
}

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

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

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

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

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

export function getLocalTokenSize(): number {
  return 130;
}
