/**
 * 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, 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 POSITION_BUNDLE_DISCRIMINATOR = new Uint8Array([129, 169, 175, 65, 185, 95, 32, 100]);

export function getPositionBundleDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode(POSITION_BUNDLE_DISCRIMINATOR); }

export type PositionBundle = { discriminator: ReadonlyUint8Array; positionBundleMint: Address; positionBitmap: ReadonlyUint8Array;  };

export type PositionBundleArgs = { positionBundleMint: Address; positionBitmap: ReadonlyUint8Array;  };

/** Gets the encoder for {@link PositionBundleArgs} account data. */
export function getPositionBundleEncoder(): FixedSizeEncoder<PositionBundleArgs> {
    return transformEncoder(getStructEncoder([['discriminator', fixEncoderSize(getBytesEncoder(), 8)], ['positionBundleMint', getAddressEncoder()], ['positionBitmap', fixEncoderSize(getBytesEncoder(), 32)]]), (value) => ({ ...value, discriminator: POSITION_BUNDLE_DISCRIMINATOR }));
}

/** Gets the decoder for {@link PositionBundle} account data. */
export function getPositionBundleDecoder(): FixedSizeDecoder<PositionBundle> {
    return getStructDecoder([['discriminator', fixDecoderSize(getBytesDecoder(), 8)], ['positionBundleMint', getAddressDecoder()], ['positionBitmap', fixDecoderSize(getBytesDecoder(), 32)]]);
}

/** Gets the codec for {@link PositionBundle} account data. */
export function getPositionBundleCodec(): FixedSizeCodec<PositionBundleArgs, PositionBundle> {
    return combineCodec(getPositionBundleEncoder(), getPositionBundleDecoder());
}

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

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

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

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

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

export function getPositionBundleSize(): number {
  return 72;
}