/**
 * 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,
  getU32Decoder,
  getU32Encoder,
  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 TOKEN_MESSENGER_DISCRIMINATOR = new Uint8Array([
  162, 4, 242, 52, 147, 243, 221, 96,
]);

export function getTokenMessengerDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    TOKEN_MESSENGER_DISCRIMINATOR,
  );
}

export type TokenMessenger = {
  discriminator: ReadonlyUint8Array;
  owner: Address;
  pendingOwner: Address;
  localMessageTransmitter: Address;
  messageBodyVersion: number;
  authorityBump: number;
};

export type TokenMessengerArgs = {
  owner: Address;
  pendingOwner: Address;
  localMessageTransmitter: Address;
  messageBodyVersion: number;
  authorityBump: number;
};

/** Gets the encoder for {@link TokenMessengerArgs} account data. */
export function getTokenMessengerEncoder(): FixedSizeEncoder<TokenMessengerArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["owner", getAddressEncoder()],
      ["pendingOwner", getAddressEncoder()],
      ["localMessageTransmitter", getAddressEncoder()],
      ["messageBodyVersion", getU32Encoder()],
      ["authorityBump", getU8Encoder()],
    ]),
    (value) => ({ ...value, discriminator: TOKEN_MESSENGER_DISCRIMINATOR }),
  );
}

/** Gets the decoder for {@link TokenMessenger} account data. */
export function getTokenMessengerDecoder(): FixedSizeDecoder<TokenMessenger> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["owner", getAddressDecoder()],
    ["pendingOwner", getAddressDecoder()],
    ["localMessageTransmitter", getAddressDecoder()],
    ["messageBodyVersion", getU32Decoder()],
    ["authorityBump", getU8Decoder()],
  ]);
}

/** Gets the codec for {@link TokenMessenger} account data. */
export function getTokenMessengerCodec(): FixedSizeCodec<
  TokenMessengerArgs,
  TokenMessenger
> {
  return combineCodec(getTokenMessengerEncoder(), getTokenMessengerDecoder());
}

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

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

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

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

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

export function getTokenMessengerSize(): number {
  return 109;
}
