/**
 * 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, getOptionDecoder, getOptionEncoder, getStructDecoder, getStructEncoder, transformEncoder, type AccountMeta, type AccountSignerMeta, type Address, type Codec, type Decoder, type Encoder, type Instruction, type InstructionWithAccounts, type InstructionWithData, type Option, type OptionOrNullable, 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';
import { getRemainingAccountsInfoDecoder, getRemainingAccountsInfoEncoder, type RemainingAccountsInfo, type RemainingAccountsInfoArgs } from '../types';

export const COLLECT_PROTOCOL_FEES_V2_DISCRIMINATOR = new Uint8Array([103, 128, 222, 134, 114, 200, 22, 200]);

export function getCollectProtocolFeesV2DiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode(COLLECT_PROTOCOL_FEES_V2_DISCRIMINATOR); }

export type CollectProtocolFeesV2Instruction<TProgram extends string = typeof WHIRLPOOL_PROGRAM_ADDRESS, TAccountWhirlpoolsConfig extends string | AccountMeta<string> = string, TAccountWhirlpool extends string | AccountMeta<string> = string, TAccountCollectProtocolFeesAuthority extends string | AccountMeta<string> = string, TAccountTokenMintA extends string | AccountMeta<string> = string, TAccountTokenMintB extends string | AccountMeta<string> = string, TAccountTokenVaultA extends string | AccountMeta<string> = string, TAccountTokenVaultB extends string | AccountMeta<string> = string, TAccountTokenDestinationA extends string | AccountMeta<string> = string, TAccountTokenDestinationB extends string | AccountMeta<string> = string, TAccountTokenProgramA extends string | AccountMeta<string> = string, TAccountTokenProgramB extends string | AccountMeta<string> = string, TAccountMemoProgram extends string | AccountMeta<string> = string, TRemainingAccounts extends readonly AccountMeta<string>[] = []> =
Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> & InstructionWithAccounts<[TAccountWhirlpoolsConfig extends string ? ReadonlyAccount<TAccountWhirlpoolsConfig> : TAccountWhirlpoolsConfig, TAccountWhirlpool extends string ? WritableAccount<TAccountWhirlpool> : TAccountWhirlpool, TAccountCollectProtocolFeesAuthority extends string ? ReadonlySignerAccount<TAccountCollectProtocolFeesAuthority> & AccountSignerMeta<TAccountCollectProtocolFeesAuthority> : TAccountCollectProtocolFeesAuthority, TAccountTokenMintA extends string ? ReadonlyAccount<TAccountTokenMintA> : TAccountTokenMintA, TAccountTokenMintB extends string ? ReadonlyAccount<TAccountTokenMintB> : TAccountTokenMintB, TAccountTokenVaultA extends string ? WritableAccount<TAccountTokenVaultA> : TAccountTokenVaultA, TAccountTokenVaultB extends string ? WritableAccount<TAccountTokenVaultB> : TAccountTokenVaultB, TAccountTokenDestinationA extends string ? WritableAccount<TAccountTokenDestinationA> : TAccountTokenDestinationA, TAccountTokenDestinationB extends string ? WritableAccount<TAccountTokenDestinationB> : TAccountTokenDestinationB, TAccountTokenProgramA extends string ? ReadonlyAccount<TAccountTokenProgramA> : TAccountTokenProgramA, TAccountTokenProgramB extends string ? ReadonlyAccount<TAccountTokenProgramB> : TAccountTokenProgramB, TAccountMemoProgram extends string ? ReadonlyAccount<TAccountMemoProgram> : TAccountMemoProgram, ...TRemainingAccounts]>;

export type CollectProtocolFeesV2InstructionData = { discriminator: ReadonlyUint8Array; remainingAccountsInfo: Option<RemainingAccountsInfo>;  };

export type CollectProtocolFeesV2InstructionDataArgs = { remainingAccountsInfo: OptionOrNullable<RemainingAccountsInfoArgs>;  };

export function getCollectProtocolFeesV2InstructionDataEncoder(): Encoder<CollectProtocolFeesV2InstructionDataArgs> {
    return transformEncoder(getStructEncoder([['discriminator', fixEncoderSize(getBytesEncoder(), 8)], ['remainingAccountsInfo', getOptionEncoder(getRemainingAccountsInfoEncoder())]]), (value) => ({ ...value, discriminator: COLLECT_PROTOCOL_FEES_V2_DISCRIMINATOR }));
}

export function getCollectProtocolFeesV2InstructionDataDecoder(): Decoder<CollectProtocolFeesV2InstructionData> {
    return getStructDecoder([['discriminator', fixDecoderSize(getBytesDecoder(), 8)], ['remainingAccountsInfo', getOptionDecoder(getRemainingAccountsInfoDecoder())]]);
}

export function getCollectProtocolFeesV2InstructionDataCodec(): Codec<CollectProtocolFeesV2InstructionDataArgs, CollectProtocolFeesV2InstructionData> {
    return combineCodec(getCollectProtocolFeesV2InstructionDataEncoder(), getCollectProtocolFeesV2InstructionDataDecoder());
}

export type CollectProtocolFeesV2Input<TAccountWhirlpoolsConfig extends string = string, TAccountWhirlpool extends string = string, TAccountCollectProtocolFeesAuthority extends string = string, TAccountTokenMintA extends string = string, TAccountTokenMintB extends string = string, TAccountTokenVaultA extends string = string, TAccountTokenVaultB extends string = string, TAccountTokenDestinationA extends string = string, TAccountTokenDestinationB extends string = string, TAccountTokenProgramA extends string = string, TAccountTokenProgramB extends string = string, TAccountMemoProgram extends string = string> =  {
  whirlpoolsConfig: Address<TAccountWhirlpoolsConfig>;
whirlpool: Address<TAccountWhirlpool>;
collectProtocolFeesAuthority: TransactionSigner<TAccountCollectProtocolFeesAuthority>;
tokenMintA: Address<TAccountTokenMintA>;
tokenMintB: Address<TAccountTokenMintB>;
tokenVaultA: Address<TAccountTokenVaultA>;
tokenVaultB: Address<TAccountTokenVaultB>;
tokenDestinationA: Address<TAccountTokenDestinationA>;
tokenDestinationB: Address<TAccountTokenDestinationB>;
tokenProgramA: Address<TAccountTokenProgramA>;
tokenProgramB: Address<TAccountTokenProgramB>;
memoProgram: Address<TAccountMemoProgram>;
remainingAccountsInfo: CollectProtocolFeesV2InstructionDataArgs["remainingAccountsInfo"];
}

export function getCollectProtocolFeesV2Instruction<TAccountWhirlpoolsConfig extends string, TAccountWhirlpool extends string, TAccountCollectProtocolFeesAuthority extends string, TAccountTokenMintA extends string, TAccountTokenMintB extends string, TAccountTokenVaultA extends string, TAccountTokenVaultB extends string, TAccountTokenDestinationA extends string, TAccountTokenDestinationB extends string, TAccountTokenProgramA extends string, TAccountTokenProgramB extends string, TAccountMemoProgram extends string, TProgramAddress extends Address = typeof WHIRLPOOL_PROGRAM_ADDRESS>(input: CollectProtocolFeesV2Input<TAccountWhirlpoolsConfig, TAccountWhirlpool, TAccountCollectProtocolFeesAuthority, TAccountTokenMintA, TAccountTokenMintB, TAccountTokenVaultA, TAccountTokenVaultB, TAccountTokenDestinationA, TAccountTokenDestinationB, TAccountTokenProgramA, TAccountTokenProgramB, TAccountMemoProgram>, config?: { programAddress?: TProgramAddress } ): CollectProtocolFeesV2Instruction<TProgramAddress, TAccountWhirlpoolsConfig, TAccountWhirlpool, TAccountCollectProtocolFeesAuthority, TAccountTokenMintA, TAccountTokenMintB, TAccountTokenVaultA, TAccountTokenVaultB, TAccountTokenDestinationA, TAccountTokenDestinationB, TAccountTokenProgramA, TAccountTokenProgramB, TAccountMemoProgram> {
  // Program address.
const programAddress = config?.programAddress ?? WHIRLPOOL_PROGRAM_ADDRESS;

 // Original accounts.
const originalAccounts = { whirlpoolsConfig: { value: input.whirlpoolsConfig ?? null, isWritable: false }, whirlpool: { value: input.whirlpool ?? null, isWritable: true }, collectProtocolFeesAuthority: { value: input.collectProtocolFeesAuthority ?? null, isWritable: false }, tokenMintA: { value: input.tokenMintA ?? null, isWritable: false }, tokenMintB: { value: input.tokenMintB ?? null, isWritable: false }, tokenVaultA: { value: input.tokenVaultA ?? null, isWritable: true }, tokenVaultB: { value: input.tokenVaultB ?? null, isWritable: true }, tokenDestinationA: { value: input.tokenDestinationA ?? null, isWritable: true }, tokenDestinationB: { value: input.tokenDestinationB ?? null, isWritable: true }, tokenProgramA: { value: input.tokenProgramA ?? null, isWritable: false }, tokenProgramB: { value: input.tokenProgramB ?? null, isWritable: false }, memoProgram: { value: input.memoProgram ?? null, isWritable: false } }
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.whirlpoolsConfig), getAccountMeta(accounts.whirlpool), getAccountMeta(accounts.collectProtocolFeesAuthority), getAccountMeta(accounts.tokenMintA), getAccountMeta(accounts.tokenMintB), getAccountMeta(accounts.tokenVaultA), getAccountMeta(accounts.tokenVaultB), getAccountMeta(accounts.tokenDestinationA), getAccountMeta(accounts.tokenDestinationB), getAccountMeta(accounts.tokenProgramA), getAccountMeta(accounts.tokenProgramB), getAccountMeta(accounts.memoProgram)], data: getCollectProtocolFeesV2InstructionDataEncoder().encode(args as CollectProtocolFeesV2InstructionDataArgs), programAddress } as CollectProtocolFeesV2Instruction<TProgramAddress, TAccountWhirlpoolsConfig, TAccountWhirlpool, TAccountCollectProtocolFeesAuthority, TAccountTokenMintA, TAccountTokenMintB, TAccountTokenVaultA, TAccountTokenVaultB, TAccountTokenDestinationA, TAccountTokenDestinationB, TAccountTokenProgramA, TAccountTokenProgramB, TAccountMemoProgram>);
}

export type ParsedCollectProtocolFeesV2Instruction<TProgram extends string = typeof WHIRLPOOL_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[]> = { programAddress: Address<TProgram>;
accounts: {
whirlpoolsConfig: TAccountMetas[0];
whirlpool: TAccountMetas[1];
collectProtocolFeesAuthority: TAccountMetas[2];
tokenMintA: TAccountMetas[3];
tokenMintB: TAccountMetas[4];
tokenVaultA: TAccountMetas[5];
tokenVaultB: TAccountMetas[6];
tokenDestinationA: TAccountMetas[7];
tokenDestinationB: TAccountMetas[8];
tokenProgramA: TAccountMetas[9];
tokenProgramB: TAccountMetas[10];
memoProgram: TAccountMetas[11];
};
data: CollectProtocolFeesV2InstructionData; };

export function parseCollectProtocolFeesV2Instruction<TProgram extends string, TAccountMetas extends readonly AccountMeta[]>(instruction: Instruction<TProgram> & InstructionWithAccounts<TAccountMetas> & InstructionWithData<ReadonlyUint8Array>): ParsedCollectProtocolFeesV2Instruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 12) {
  // 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: { whirlpoolsConfig: getNextAccount(), whirlpool: getNextAccount(), collectProtocolFeesAuthority: getNextAccount(), tokenMintA: getNextAccount(), tokenMintB: getNextAccount(), tokenVaultA: getNextAccount(), tokenVaultB: getNextAccount(), tokenDestinationA: getNextAccount(), tokenDestinationB: getNextAccount(), tokenProgramA: getNextAccount(), tokenProgramB: getNextAccount(), memoProgram: getNextAccount() }, data: getCollectProtocolFeesV2InstructionDataDecoder().decode(instruction.data) };
}