/**
 * 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, getU64Decoder, getU64Encoder, transformEncoder, type AccountMeta, type Address, type FixedSizeCodec, type FixedSizeDecoder, type FixedSizeEncoder, type Instruction, type InstructionWithAccounts, type InstructionWithData, type ReadonlyAccount, type ReadonlyUint8Array, type WritableAccount } from '@solana/kit';
import { LB_CLMM_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';

export const WITHDRAW_PROTOCOL_FEE_DISCRIMINATOR = new Uint8Array([158, 201, 158, 189, 33, 93, 162, 103]);

export function getWithdrawProtocolFeeDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode(WITHDRAW_PROTOCOL_FEE_DISCRIMINATOR); }

export type WithdrawProtocolFeeInstruction<TProgram extends string = typeof LB_CLMM_PROGRAM_ADDRESS, TAccountLbPair extends string | AccountMeta<string> = string, TAccountReserveX extends string | AccountMeta<string> = string, TAccountReserveY extends string | AccountMeta<string> = string, TAccountTokenXMint extends string | AccountMeta<string> = string, TAccountTokenYMint extends string | AccountMeta<string> = string, TAccountReceiverTokenX extends string | AccountMeta<string> = string, TAccountReceiverTokenY extends string | AccountMeta<string> = string, TAccountTokenXProgram extends string | AccountMeta<string> = string, TAccountTokenYProgram extends string | AccountMeta<string> = string, TRemainingAccounts extends readonly AccountMeta<string>[] = []> =
Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> & InstructionWithAccounts<[TAccountLbPair extends string ? WritableAccount<TAccountLbPair> : TAccountLbPair, TAccountReserveX extends string ? WritableAccount<TAccountReserveX> : TAccountReserveX, TAccountReserveY extends string ? WritableAccount<TAccountReserveY> : TAccountReserveY, TAccountTokenXMint extends string ? ReadonlyAccount<TAccountTokenXMint> : TAccountTokenXMint, TAccountTokenYMint extends string ? ReadonlyAccount<TAccountTokenYMint> : TAccountTokenYMint, TAccountReceiverTokenX extends string ? WritableAccount<TAccountReceiverTokenX> : TAccountReceiverTokenX, TAccountReceiverTokenY extends string ? WritableAccount<TAccountReceiverTokenY> : TAccountReceiverTokenY, TAccountTokenXProgram extends string ? ReadonlyAccount<TAccountTokenXProgram> : TAccountTokenXProgram, TAccountTokenYProgram extends string ? ReadonlyAccount<TAccountTokenYProgram> : TAccountTokenYProgram, ...TRemainingAccounts]>;

export type WithdrawProtocolFeeInstructionData = { discriminator: ReadonlyUint8Array; amountX: bigint; amountY: bigint;  };

export type WithdrawProtocolFeeInstructionDataArgs = { amountX: number | bigint; amountY: number | bigint;  };

export function getWithdrawProtocolFeeInstructionDataEncoder(): FixedSizeEncoder<WithdrawProtocolFeeInstructionDataArgs> {
    return transformEncoder(getStructEncoder([['discriminator', fixEncoderSize(getBytesEncoder(), 8)], ['amountX', getU64Encoder()], ['amountY', getU64Encoder()]]), (value) => ({ ...value, discriminator: WITHDRAW_PROTOCOL_FEE_DISCRIMINATOR }));
}

export function getWithdrawProtocolFeeInstructionDataDecoder(): FixedSizeDecoder<WithdrawProtocolFeeInstructionData> {
    return getStructDecoder([['discriminator', fixDecoderSize(getBytesDecoder(), 8)], ['amountX', getU64Decoder()], ['amountY', getU64Decoder()]]);
}

export function getWithdrawProtocolFeeInstructionDataCodec(): FixedSizeCodec<WithdrawProtocolFeeInstructionDataArgs, WithdrawProtocolFeeInstructionData> {
    return combineCodec(getWithdrawProtocolFeeInstructionDataEncoder(), getWithdrawProtocolFeeInstructionDataDecoder());
}

export type WithdrawProtocolFeeInput<TAccountLbPair extends string = string, TAccountReserveX extends string = string, TAccountReserveY extends string = string, TAccountTokenXMint extends string = string, TAccountTokenYMint extends string = string, TAccountReceiverTokenX extends string = string, TAccountReceiverTokenY extends string = string, TAccountTokenXProgram extends string = string, TAccountTokenYProgram extends string = string> =  {
  lbPair: Address<TAccountLbPair>;
reserveX: Address<TAccountReserveX>;
reserveY: Address<TAccountReserveY>;
tokenXMint: Address<TAccountTokenXMint>;
tokenYMint: Address<TAccountTokenYMint>;
receiverTokenX: Address<TAccountReceiverTokenX>;
receiverTokenY: Address<TAccountReceiverTokenY>;
tokenXProgram: Address<TAccountTokenXProgram>;
tokenYProgram: Address<TAccountTokenYProgram>;
amountX: WithdrawProtocolFeeInstructionDataArgs["amountX"];
amountY: WithdrawProtocolFeeInstructionDataArgs["amountY"];
}

export function getWithdrawProtocolFeeInstruction<TAccountLbPair extends string, TAccountReserveX extends string, TAccountReserveY extends string, TAccountTokenXMint extends string, TAccountTokenYMint extends string, TAccountReceiverTokenX extends string, TAccountReceiverTokenY extends string, TAccountTokenXProgram extends string, TAccountTokenYProgram extends string, TProgramAddress extends Address = typeof LB_CLMM_PROGRAM_ADDRESS>(input: WithdrawProtocolFeeInput<TAccountLbPair, TAccountReserveX, TAccountReserveY, TAccountTokenXMint, TAccountTokenYMint, TAccountReceiverTokenX, TAccountReceiverTokenY, TAccountTokenXProgram, TAccountTokenYProgram>, config?: { programAddress?: TProgramAddress } ): WithdrawProtocolFeeInstruction<TProgramAddress, TAccountLbPair, TAccountReserveX, TAccountReserveY, TAccountTokenXMint, TAccountTokenYMint, TAccountReceiverTokenX, TAccountReceiverTokenY, TAccountTokenXProgram, TAccountTokenYProgram> {
  // Program address.
const programAddress = config?.programAddress ?? LB_CLMM_PROGRAM_ADDRESS;

 // Original accounts.
const originalAccounts = { lbPair: { value: input.lbPair ?? null, isWritable: true }, reserveX: { value: input.reserveX ?? null, isWritable: true }, reserveY: { value: input.reserveY ?? null, isWritable: true }, tokenXMint: { value: input.tokenXMint ?? null, isWritable: false }, tokenYMint: { value: input.tokenYMint ?? null, isWritable: false }, receiverTokenX: { value: input.receiverTokenX ?? null, isWritable: true }, receiverTokenY: { value: input.receiverTokenY ?? null, isWritable: true }, tokenXProgram: { value: input.tokenXProgram ?? null, isWritable: false }, tokenYProgram: { value: input.tokenYProgram ?? 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.lbPair), getAccountMeta(accounts.reserveX), getAccountMeta(accounts.reserveY), getAccountMeta(accounts.tokenXMint), getAccountMeta(accounts.tokenYMint), getAccountMeta(accounts.receiverTokenX), getAccountMeta(accounts.receiverTokenY), getAccountMeta(accounts.tokenXProgram), getAccountMeta(accounts.tokenYProgram)], data: getWithdrawProtocolFeeInstructionDataEncoder().encode(args as WithdrawProtocolFeeInstructionDataArgs), programAddress } as WithdrawProtocolFeeInstruction<TProgramAddress, TAccountLbPair, TAccountReserveX, TAccountReserveY, TAccountTokenXMint, TAccountTokenYMint, TAccountReceiverTokenX, TAccountReceiverTokenY, TAccountTokenXProgram, TAccountTokenYProgram>);
}

export type ParsedWithdrawProtocolFeeInstruction<TProgram extends string = typeof LB_CLMM_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[]> = { programAddress: Address<TProgram>;
accounts: {
lbPair: TAccountMetas[0];
reserveX: TAccountMetas[1];
reserveY: TAccountMetas[2];
tokenXMint: TAccountMetas[3];
tokenYMint: TAccountMetas[4];
receiverTokenX: TAccountMetas[5];
receiverTokenY: TAccountMetas[6];
tokenXProgram: TAccountMetas[7];
tokenYProgram: TAccountMetas[8];
};
data: WithdrawProtocolFeeInstructionData; };

export function parseWithdrawProtocolFeeInstruction<TProgram extends string, TAccountMetas extends readonly AccountMeta[]>(instruction: Instruction<TProgram> & InstructionWithAccounts<TAccountMetas> & InstructionWithData<ReadonlyUint8Array>): ParsedWithdrawProtocolFeeInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 9) {
  // 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: { lbPair: getNextAccount(), reserveX: getNextAccount(), reserveY: getNextAccount(), tokenXMint: getNextAccount(), tokenYMint: getNextAccount(), receiverTokenX: getNextAccount(), receiverTokenY: getNextAccount(), tokenXProgram: getNextAccount(), tokenYProgram: getNextAccount() }, data: getWithdrawProtocolFeeInstructionDataDecoder().decode(instruction.data) };
}