/**
 * 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, 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 { LB_CLMM_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';

export const CLOSE_POSITION_DISCRIMINATOR = new Uint8Array([123, 134, 81, 0, 49, 68, 98, 98]);

export function getClosePositionDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode(CLOSE_POSITION_DISCRIMINATOR); }

export type ClosePositionInstruction<TProgram extends string = typeof LB_CLMM_PROGRAM_ADDRESS, TAccountPosition extends string | AccountMeta<string> = string, TAccountLbPair extends string | AccountMeta<string> = string, TAccountBinArrayLower extends string | AccountMeta<string> = string, TAccountBinArrayUpper extends string | AccountMeta<string> = string, TAccountSender extends string | AccountMeta<string> = string, TAccountRentReceiver extends string | AccountMeta<string> = string, TAccountEventAuthority extends string | AccountMeta<string> = string, TAccountProgram extends string | AccountMeta<string> = string, TRemainingAccounts extends readonly AccountMeta<string>[] = []> =
Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> & InstructionWithAccounts<[TAccountPosition extends string ? WritableAccount<TAccountPosition> : TAccountPosition, TAccountLbPair extends string ? WritableAccount<TAccountLbPair> : TAccountLbPair, TAccountBinArrayLower extends string ? WritableAccount<TAccountBinArrayLower> : TAccountBinArrayLower, TAccountBinArrayUpper extends string ? WritableAccount<TAccountBinArrayUpper> : TAccountBinArrayUpper, TAccountSender extends string ? ReadonlySignerAccount<TAccountSender> & AccountSignerMeta<TAccountSender> : TAccountSender, TAccountRentReceiver extends string ? WritableAccount<TAccountRentReceiver> : TAccountRentReceiver, TAccountEventAuthority extends string ? ReadonlyAccount<TAccountEventAuthority> : TAccountEventAuthority, TAccountProgram extends string ? ReadonlyAccount<TAccountProgram> : TAccountProgram, ...TRemainingAccounts]>;

export type ClosePositionInstructionData = { discriminator: ReadonlyUint8Array;  };

export type ClosePositionInstructionDataArgs = {  };

export function getClosePositionInstructionDataEncoder(): FixedSizeEncoder<ClosePositionInstructionDataArgs> {
    return transformEncoder(getStructEncoder([['discriminator', fixEncoderSize(getBytesEncoder(), 8)]]), (value) => ({ ...value, discriminator: CLOSE_POSITION_DISCRIMINATOR }));
}

export function getClosePositionInstructionDataDecoder(): FixedSizeDecoder<ClosePositionInstructionData> {
    return getStructDecoder([['discriminator', fixDecoderSize(getBytesDecoder(), 8)]]);
}

export function getClosePositionInstructionDataCodec(): FixedSizeCodec<ClosePositionInstructionDataArgs, ClosePositionInstructionData> {
    return combineCodec(getClosePositionInstructionDataEncoder(), getClosePositionInstructionDataDecoder());
}

export type ClosePositionInput<TAccountPosition extends string = string, TAccountLbPair extends string = string, TAccountBinArrayLower extends string = string, TAccountBinArrayUpper extends string = string, TAccountSender extends string = string, TAccountRentReceiver extends string = string, TAccountEventAuthority extends string = string, TAccountProgram extends string = string> =  {
  position: Address<TAccountPosition>;
lbPair: Address<TAccountLbPair>;
binArrayLower: Address<TAccountBinArrayLower>;
binArrayUpper: Address<TAccountBinArrayUpper>;
sender: TransactionSigner<TAccountSender>;
rentReceiver: Address<TAccountRentReceiver>;
eventAuthority: Address<TAccountEventAuthority>;
program: Address<TAccountProgram>;
}

export function getClosePositionInstruction<TAccountPosition extends string, TAccountLbPair extends string, TAccountBinArrayLower extends string, TAccountBinArrayUpper extends string, TAccountSender extends string, TAccountRentReceiver extends string, TAccountEventAuthority extends string, TAccountProgram extends string, TProgramAddress extends Address = typeof LB_CLMM_PROGRAM_ADDRESS>(input: ClosePositionInput<TAccountPosition, TAccountLbPair, TAccountBinArrayLower, TAccountBinArrayUpper, TAccountSender, TAccountRentReceiver, TAccountEventAuthority, TAccountProgram>, config?: { programAddress?: TProgramAddress } ): ClosePositionInstruction<TProgramAddress, TAccountPosition, TAccountLbPair, TAccountBinArrayLower, TAccountBinArrayUpper, TAccountSender, TAccountRentReceiver, TAccountEventAuthority, TAccountProgram> {
  // Program address.
const programAddress = config?.programAddress ?? LB_CLMM_PROGRAM_ADDRESS;

 // Original accounts.
const originalAccounts = { position: { value: input.position ?? null, isWritable: true }, lbPair: { value: input.lbPair ?? null, isWritable: true }, binArrayLower: { value: input.binArrayLower ?? null, isWritable: true }, binArrayUpper: { value: input.binArrayUpper ?? null, isWritable: true }, sender: { value: input.sender ?? null, isWritable: false }, rentReceiver: { value: input.rentReceiver ?? null, isWritable: true }, eventAuthority: { value: input.eventAuthority ?? null, isWritable: false }, program: { value: input.program ?? null, isWritable: false } }
const accounts = originalAccounts as Record<keyof typeof originalAccounts, ResolvedAccount>;




const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
return Object.freeze({ accounts: [getAccountMeta(accounts.position), getAccountMeta(accounts.lbPair), getAccountMeta(accounts.binArrayLower), getAccountMeta(accounts.binArrayUpper), getAccountMeta(accounts.sender), getAccountMeta(accounts.rentReceiver), getAccountMeta(accounts.eventAuthority), getAccountMeta(accounts.program)], data: getClosePositionInstructionDataEncoder().encode({}), programAddress } as ClosePositionInstruction<TProgramAddress, TAccountPosition, TAccountLbPair, TAccountBinArrayLower, TAccountBinArrayUpper, TAccountSender, TAccountRentReceiver, TAccountEventAuthority, TAccountProgram>);
}

export type ParsedClosePositionInstruction<TProgram extends string = typeof LB_CLMM_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[]> = { programAddress: Address<TProgram>;
accounts: {
position: TAccountMetas[0];
lbPair: TAccountMetas[1];
binArrayLower: TAccountMetas[2];
binArrayUpper: TAccountMetas[3];
sender: TAccountMetas[4];
rentReceiver: TAccountMetas[5];
eventAuthority: TAccountMetas[6];
program: TAccountMetas[7];
};
data: ClosePositionInstructionData; };

export function parseClosePositionInstruction<TProgram extends string, TAccountMetas extends readonly AccountMeta[]>(instruction: Instruction<TProgram> & InstructionWithAccounts<TAccountMetas> & InstructionWithData<ReadonlyUint8Array>): ParsedClosePositionInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 8) {
  // 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: { position: getNextAccount(), lbPair: getNextAccount(), binArrayLower: getNextAccount(), binArrayUpper: getNextAccount(), sender: getNextAccount(), rentReceiver: getNextAccount(), eventAuthority: getNextAccount(), program: getNextAccount() }, data: getClosePositionInstructionDataDecoder().decode(instruction.data) };
}