/**
 * 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 { combineCodec, fixDecoderSize, fixEncoderSize, getBytesDecoder, getBytesEncoder, getStructDecoder, getStructEncoder, getU16Decoder, getU16Encoder, transformEncoder, type AccountMeta, type AccountSignerMeta, type Address, type FixedSizeCodec, type FixedSizeDecoder, type FixedSizeEncoder, type Instruction, type InstructionWithAccounts, type InstructionWithData, type ReadonlyAccount, type ReadonlySignerAccount, type ReadonlyUint8Array, type TransactionSigner, type WritableAccount } from '@solana/kit';
import { WHIRLPOOL_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';

export const CLOSE_BUNDLED_POSITION_DISCRIMINATOR = new Uint8Array([41, 36, 216, 245, 27, 85, 103, 67]);

export function getCloseBundledPositionDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode(CLOSE_BUNDLED_POSITION_DISCRIMINATOR); }

export type CloseBundledPositionInstruction<TProgram extends string = typeof WHIRLPOOL_PROGRAM_ADDRESS, TAccountBundledPosition extends string | AccountMeta<string> = string, TAccountPositionBundle extends string | AccountMeta<string> = string, TAccountPositionBundleTokenAccount extends string | AccountMeta<string> = string, TAccountPositionBundleAuthority extends string | AccountMeta<string> = string, TAccountReceiver extends string | AccountMeta<string> = string, TRemainingAccounts extends readonly AccountMeta<string>[] = []> =
Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> & InstructionWithAccounts<[TAccountBundledPosition extends string ? WritableAccount<TAccountBundledPosition> : TAccountBundledPosition, TAccountPositionBundle extends string ? WritableAccount<TAccountPositionBundle> : TAccountPositionBundle, TAccountPositionBundleTokenAccount extends string ? ReadonlyAccount<TAccountPositionBundleTokenAccount> : TAccountPositionBundleTokenAccount, TAccountPositionBundleAuthority extends string ? ReadonlySignerAccount<TAccountPositionBundleAuthority> & AccountSignerMeta<TAccountPositionBundleAuthority> : TAccountPositionBundleAuthority, TAccountReceiver extends string ? WritableAccount<TAccountReceiver> : TAccountReceiver, ...TRemainingAccounts]>;

export type CloseBundledPositionInstructionData = { discriminator: ReadonlyUint8Array; bundleIndex: number;  };

export type CloseBundledPositionInstructionDataArgs = { bundleIndex: number;  };

export function getCloseBundledPositionInstructionDataEncoder(): FixedSizeEncoder<CloseBundledPositionInstructionDataArgs> {
    return transformEncoder(getStructEncoder([['discriminator', fixEncoderSize(getBytesEncoder(), 8)], ['bundleIndex', getU16Encoder()]]), (value) => ({ ...value, discriminator: CLOSE_BUNDLED_POSITION_DISCRIMINATOR }));
}

export function getCloseBundledPositionInstructionDataDecoder(): FixedSizeDecoder<CloseBundledPositionInstructionData> {
    return getStructDecoder([['discriminator', fixDecoderSize(getBytesDecoder(), 8)], ['bundleIndex', getU16Decoder()]]);
}

export function getCloseBundledPositionInstructionDataCodec(): FixedSizeCodec<CloseBundledPositionInstructionDataArgs, CloseBundledPositionInstructionData> {
    return combineCodec(getCloseBundledPositionInstructionDataEncoder(), getCloseBundledPositionInstructionDataDecoder());
}

export type CloseBundledPositionInput<TAccountBundledPosition extends string = string, TAccountPositionBundle extends string = string, TAccountPositionBundleTokenAccount extends string = string, TAccountPositionBundleAuthority extends string = string, TAccountReceiver extends string = string> =  {
  bundledPosition: Address<TAccountBundledPosition>;
positionBundle: Address<TAccountPositionBundle>;
positionBundleTokenAccount: Address<TAccountPositionBundleTokenAccount>;
positionBundleAuthority: TransactionSigner<TAccountPositionBundleAuthority>;
receiver: Address<TAccountReceiver>;
bundleIndex: CloseBundledPositionInstructionDataArgs["bundleIndex"];
}

export function getCloseBundledPositionInstruction<TAccountBundledPosition extends string, TAccountPositionBundle extends string, TAccountPositionBundleTokenAccount extends string, TAccountPositionBundleAuthority extends string, TAccountReceiver extends string, TProgramAddress extends Address = typeof WHIRLPOOL_PROGRAM_ADDRESS>(input: CloseBundledPositionInput<TAccountBundledPosition, TAccountPositionBundle, TAccountPositionBundleTokenAccount, TAccountPositionBundleAuthority, TAccountReceiver>, config?: { programAddress?: TProgramAddress } ): CloseBundledPositionInstruction<TProgramAddress, TAccountBundledPosition, TAccountPositionBundle, TAccountPositionBundleTokenAccount, TAccountPositionBundleAuthority, TAccountReceiver> {
  // Program address.
const programAddress = config?.programAddress ?? WHIRLPOOL_PROGRAM_ADDRESS;

 // Original accounts.
const originalAccounts = { bundledPosition: { value: input.bundledPosition ?? null, isWritable: true }, positionBundle: { value: input.positionBundle ?? null, isWritable: true }, positionBundleTokenAccount: { value: input.positionBundleTokenAccount ?? null, isWritable: false }, positionBundleAuthority: { value: input.positionBundleAuthority ?? null, isWritable: false }, receiver: { value: input.receiver ?? null, isWritable: true } }
const accounts = originalAccounts as Record<keyof typeof originalAccounts, ResolvedAccount>;


// Original args.
const args = { ...input,  };




const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
return Object.freeze({ accounts: [getAccountMeta(accounts.bundledPosition), getAccountMeta(accounts.positionBundle), getAccountMeta(accounts.positionBundleTokenAccount), getAccountMeta(accounts.positionBundleAuthority), getAccountMeta(accounts.receiver)], data: getCloseBundledPositionInstructionDataEncoder().encode(args as CloseBundledPositionInstructionDataArgs), programAddress } as CloseBundledPositionInstruction<TProgramAddress, TAccountBundledPosition, TAccountPositionBundle, TAccountPositionBundleTokenAccount, TAccountPositionBundleAuthority, TAccountReceiver>);
}

export type ParsedCloseBundledPositionInstruction<TProgram extends string = typeof WHIRLPOOL_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[]> = { programAddress: Address<TProgram>;
accounts: {
bundledPosition: TAccountMetas[0];
positionBundle: TAccountMetas[1];
positionBundleTokenAccount: TAccountMetas[2];
positionBundleAuthority: TAccountMetas[3];
receiver: TAccountMetas[4];
};
data: CloseBundledPositionInstructionData; };

export function parseCloseBundledPositionInstruction<TProgram extends string, TAccountMetas extends readonly AccountMeta[]>(instruction: Instruction<TProgram> & InstructionWithAccounts<TAccountMetas> & InstructionWithData<ReadonlyUint8Array>): ParsedCloseBundledPositionInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 5) {
  // TODO: Coded error.
  throw new Error('Not enough accounts');
}
let accountIndex = 0;
const getNextAccount = () => {
  const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
  accountIndex += 1;
  return accountMeta;
}
  return { programAddress: instruction.programAddress, accounts: { bundledPosition: getNextAccount(), positionBundle: getNextAccount(), positionBundleTokenAccount: getNextAccount(), positionBundleAuthority: getNextAccount(), receiver: getNextAccount() }, data: getCloseBundledPositionInstructionDataDecoder().decode(instruction.data) };
}