/**
 * 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,
  getArrayDecoder,
  getArrayEncoder,
  getBooleanDecoder,
  getBooleanEncoder,
  getBytesDecoder,
  getBytesEncoder,
  getStructDecoder,
  getStructEncoder,
  getU32Decoder,
  getU32Encoder,
  getU64Decoder,
  getU64Encoder,
  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_TRANSMITTER_DISCRIMINATOR = new Uint8Array([
  71, 40, 180, 142, 19, 203, 35, 252,
]);

export function getMessageTransmitterDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    MESSAGE_TRANSMITTER_DISCRIMINATOR,
  );
}

export type MessageTransmitter = {
  discriminator: ReadonlyUint8Array;
  owner: Address;
  pendingOwner: Address;
  attesterManager: Address;
  pauser: Address;
  paused: boolean;
  localDomain: number;
  version: number;
  signatureThreshold: number;
  enabledAttesters: Array<Address>;
  maxMessageBodySize: bigint;
};

export type MessageTransmitterArgs = {
  owner: Address;
  pendingOwner: Address;
  attesterManager: Address;
  pauser: Address;
  paused: boolean;
  localDomain: number;
  version: number;
  signatureThreshold: number;
  enabledAttesters: Array<Address>;
  maxMessageBodySize: number | bigint;
};

/** Gets the encoder for {@link MessageTransmitterArgs} account data. */
export function getMessageTransmitterEncoder(): Encoder<MessageTransmitterArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["owner", getAddressEncoder()],
      ["pendingOwner", getAddressEncoder()],
      ["attesterManager", getAddressEncoder()],
      ["pauser", getAddressEncoder()],
      ["paused", getBooleanEncoder()],
      ["localDomain", getU32Encoder()],
      ["version", getU32Encoder()],
      ["signatureThreshold", getU32Encoder()],
      ["enabledAttesters", getArrayEncoder(getAddressEncoder())],
      ["maxMessageBodySize", getU64Encoder()],
    ]),
    (value) => ({ ...value, discriminator: MESSAGE_TRANSMITTER_DISCRIMINATOR }),
  );
}

/** Gets the decoder for {@link MessageTransmitter} account data. */
export function getMessageTransmitterDecoder(): Decoder<MessageTransmitter> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["owner", getAddressDecoder()],
    ["pendingOwner", getAddressDecoder()],
    ["attesterManager", getAddressDecoder()],
    ["pauser", getAddressDecoder()],
    ["paused", getBooleanDecoder()],
    ["localDomain", getU32Decoder()],
    ["version", getU32Decoder()],
    ["signatureThreshold", getU32Decoder()],
    ["enabledAttesters", getArrayDecoder(getAddressDecoder())],
    ["maxMessageBodySize", getU64Decoder()],
  ]);
}

/** Gets the codec for {@link MessageTransmitter} account data. */
export function getMessageTransmitterCodec(): Codec<
  MessageTransmitterArgs,
  MessageTransmitter
> {
  return combineCodec(
    getMessageTransmitterEncoder(),
    getMessageTransmitterDecoder(),
  );
}

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

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

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

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

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