/**
 * 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, getBytesDecoder, getBytesEncoder, getI32Decoder, getI32Encoder, getI64Decoder, getI64Encoder, getStructDecoder, getStructEncoder, getU64Decoder, getU64Encoder, 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';
import { getFeeInfoDecoder, getFeeInfoEncoder, getUserRewardInfoDecoder, getUserRewardInfoEncoder, type FeeInfo, type FeeInfoArgs, type UserRewardInfo, type UserRewardInfoArgs } from '../types';

export const POSITION_DISCRIMINATOR = new Uint8Array([170, 188, 143, 228, 122, 64, 247, 208]);

export function getPositionDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode(POSITION_DISCRIMINATOR); }

export type Position = { discriminator: ReadonlyUint8Array; 
/** The LB pair of this position */
lbPair: Address; 
/** Owner of the position. Client rely on this to to fetch their positions. */
owner: Address; 
/** Liquidity shares of this position in bins (lower_bin_id <-> upper_bin_id). This is the same as LP concept. */
liquidityShares: Array<bigint>; 
/** Farming reward information */
rewardInfos: Array<UserRewardInfo>; 
/** Swap fee to claim information */
feeInfos: Array<FeeInfo>; 
/** Lower bin ID */
lowerBinId: number; 
/** Upper bin ID */
upperBinId: number; 
/** Last updated timestamp */
lastUpdatedAt: bigint; 
/** Total claimed token fee X */
totalClaimedFeeXAmount: bigint; 
/** Total claimed token fee Y */
totalClaimedFeeYAmount: bigint; 
/** Total claimed rewards */
totalClaimedRewards: Array<bigint>; 
/** Reserved space for future use */
reserved: ReadonlyUint8Array;  };

export type PositionArgs = { 
/** The LB pair of this position */
lbPair: Address; 
/** Owner of the position. Client rely on this to to fetch their positions. */
owner: Address; 
/** Liquidity shares of this position in bins (lower_bin_id <-> upper_bin_id). This is the same as LP concept. */
liquidityShares: Array<number | bigint>; 
/** Farming reward information */
rewardInfos: Array<UserRewardInfoArgs>; 
/** Swap fee to claim information */
feeInfos: Array<FeeInfoArgs>; 
/** Lower bin ID */
lowerBinId: number; 
/** Upper bin ID */
upperBinId: number; 
/** Last updated timestamp */
lastUpdatedAt: number | bigint; 
/** Total claimed token fee X */
totalClaimedFeeXAmount: number | bigint; 
/** Total claimed token fee Y */
totalClaimedFeeYAmount: number | bigint; 
/** Total claimed rewards */
totalClaimedRewards: Array<number | bigint>; 
/** Reserved space for future use */
reserved: ReadonlyUint8Array;  };

/** Gets the encoder for {@link PositionArgs} account data. */
export function getPositionEncoder(): FixedSizeEncoder<PositionArgs> {
    return transformEncoder(getStructEncoder([['discriminator', fixEncoderSize(getBytesEncoder(), 8)], ['lbPair', getAddressEncoder()], ['owner', getAddressEncoder()], ['liquidityShares', getArrayEncoder(getU64Encoder(), { size: 70 })], ['rewardInfos', getArrayEncoder(getUserRewardInfoEncoder(), { size: 70 })], ['feeInfos', getArrayEncoder(getFeeInfoEncoder(), { size: 70 })], ['lowerBinId', getI32Encoder()], ['upperBinId', getI32Encoder()], ['lastUpdatedAt', getI64Encoder()], ['totalClaimedFeeXAmount', getU64Encoder()], ['totalClaimedFeeYAmount', getU64Encoder()], ['totalClaimedRewards', getArrayEncoder(getU64Encoder(), { size: 2 })], ['reserved', fixEncoderSize(getBytesEncoder(), 160)]]), (value) => ({ ...value, discriminator: POSITION_DISCRIMINATOR }));
}

/** Gets the decoder for {@link Position} account data. */
export function getPositionDecoder(): FixedSizeDecoder<Position> {
    return getStructDecoder([['discriminator', fixDecoderSize(getBytesDecoder(), 8)], ['lbPair', getAddressDecoder()], ['owner', getAddressDecoder()], ['liquidityShares', getArrayDecoder(getU64Decoder(), { size: 70 })], ['rewardInfos', getArrayDecoder(getUserRewardInfoDecoder(), { size: 70 })], ['feeInfos', getArrayDecoder(getFeeInfoDecoder(), { size: 70 })], ['lowerBinId', getI32Decoder()], ['upperBinId', getI32Decoder()], ['lastUpdatedAt', getI64Decoder()], ['totalClaimedFeeXAmount', getU64Decoder()], ['totalClaimedFeeYAmount', getU64Decoder()], ['totalClaimedRewards', getArrayDecoder(getU64Decoder(), { size: 2 })], ['reserved', fixDecoderSize(getBytesDecoder(), 160)]]);
}

/** Gets the codec for {@link Position} account data. */
export function getPositionCodec(): FixedSizeCodec<PositionArgs, Position> {
    return combineCodec(getPositionEncoder(), getPositionDecoder());
}

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

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

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

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

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

export function getPositionSize(): number {
  return 7560;
}