/**
 * 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,
  getAddressDecoder,
  getAddressEncoder,
  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 MESSAGE_SENT_DISCRIMINATOR = new Uint8Array([
  131, 100, 133, 56, 166, 225, 151, 60,
]);

export function getMessageSentDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    MESSAGE_SENT_DISCRIMINATOR,
  );
}

export type MessageSent = {
  discriminator: ReadonlyUint8Array;
  rentPayer: Address;
  message: ReadonlyUint8Array;
};

export type MessageSentArgs = {
  rentPayer: Address;
  message: ReadonlyUint8Array;
};

/** Gets the encoder for {@link MessageSentArgs} account data. */
export function getMessageSentEncoder(): Encoder<MessageSentArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["rentPayer", getAddressEncoder()],
      ["message", addEncoderSizePrefix(getBytesEncoder(), getU32Encoder())],
    ]),
    (value) => ({ ...value, discriminator: MESSAGE_SENT_DISCRIMINATOR }),
  );
}

/** Gets the decoder for {@link MessageSent} account data. */
export function getMessageSentDecoder(): Decoder<MessageSent> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["rentPayer", getAddressDecoder()],
    ["message", addDecoderSizePrefix(getBytesDecoder(), getU32Decoder())],
  ]);
}

/** Gets the codec for {@link MessageSent} account data. */
export function getMessageSentCodec(): Codec<MessageSentArgs, MessageSent> {
  return combineCodec(getMessageSentEncoder(), getMessageSentDecoder());
}

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

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

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

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

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