/**
 * 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,
  getStructDecoder,
  getStructEncoder,
  getU8Decoder,
  getU8Encoder,
  type Account,
  type Address,
  type EncodedAccount,
  type FetchAccountConfig,
  type FetchAccountsConfig,
  type FixedSizeCodec,
  type FixedSizeDecoder,
  type FixedSizeEncoder,
  type MaybeAccount,
  type MaybeEncodedAccount,
} from "@solana/kit";

export type Operator = { discriminator: number; bump: number };

export type OperatorArgs = Operator;

/** Gets the encoder for {@link OperatorArgs} account data. */
export function getOperatorEncoder(): FixedSizeEncoder<OperatorArgs> {
  return getStructEncoder([
    ["discriminator", getU8Encoder()],
    ["bump", getU8Encoder()],
  ]);
}

/** Gets the decoder for {@link Operator} account data. */
export function getOperatorDecoder(): FixedSizeDecoder<Operator> {
  return getStructDecoder([
    ["discriminator", getU8Decoder()],
    ["bump", getU8Decoder()],
  ]);
}

/** Gets the codec for {@link Operator} account data. */
export function getOperatorCodec(): FixedSizeCodec<OperatorArgs, Operator> {
  return combineCodec(getOperatorEncoder(), getOperatorDecoder());
}

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

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

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

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

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