/**
 * 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, getU128Decoder, getU128Encoder, getU64Decoder, getU64Encoder, 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 DECREASE_LIQUIDITY_DISCRIMINATOR = new Uint8Array([160, 38, 208, 111, 104, 91, 44, 1]);

export function getDecreaseLiquidityDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode(DECREASE_LIQUIDITY_DISCRIMINATOR); }

export type DecreaseLiquidityInstruction<TProgram extends string = typeof WHIRLPOOL_PROGRAM_ADDRESS, TAccountWhirlpool extends string | AccountMeta<string> = string, TAccountTokenProgram extends string | AccountMeta<string> = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", TAccountPositionAuthority extends string | AccountMeta<string> = string, TAccountPosition extends string | AccountMeta<string> = string, TAccountPositionTokenAccount extends string | AccountMeta<string> = string, TAccountTokenOwnerAccountA extends string | AccountMeta<string> = string, TAccountTokenOwnerAccountB extends string | AccountMeta<string> = string, TAccountTokenVaultA extends string | AccountMeta<string> = string, TAccountTokenVaultB extends string | AccountMeta<string> = string, TAccountTickArrayLower extends string | AccountMeta<string> = string, TAccountTickArrayUpper extends string | AccountMeta<string> = string, TRemainingAccounts extends readonly AccountMeta<string>[] = []> =
Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> & InstructionWithAccounts<[TAccountWhirlpool extends string ? WritableAccount<TAccountWhirlpool> : TAccountWhirlpool, TAccountTokenProgram extends string ? ReadonlyAccount<TAccountTokenProgram> : TAccountTokenProgram, TAccountPositionAuthority extends string ? ReadonlySignerAccount<TAccountPositionAuthority> & AccountSignerMeta<TAccountPositionAuthority> : TAccountPositionAuthority, TAccountPosition extends string ? WritableAccount<TAccountPosition> : TAccountPosition, TAccountPositionTokenAccount extends string ? ReadonlyAccount<TAccountPositionTokenAccount> : TAccountPositionTokenAccount, TAccountTokenOwnerAccountA extends string ? WritableAccount<TAccountTokenOwnerAccountA> : TAccountTokenOwnerAccountA, TAccountTokenOwnerAccountB extends string ? WritableAccount<TAccountTokenOwnerAccountB> : TAccountTokenOwnerAccountB, TAccountTokenVaultA extends string ? WritableAccount<TAccountTokenVaultA> : TAccountTokenVaultA, TAccountTokenVaultB extends string ? WritableAccount<TAccountTokenVaultB> : TAccountTokenVaultB, TAccountTickArrayLower extends string ? WritableAccount<TAccountTickArrayLower> : TAccountTickArrayLower, TAccountTickArrayUpper extends string ? WritableAccount<TAccountTickArrayUpper> : TAccountTickArrayUpper, ...TRemainingAccounts]>;

export type DecreaseLiquidityInstructionData = { discriminator: ReadonlyUint8Array; liquidityAmount: bigint; tokenMinA: bigint; tokenMinB: bigint;  };

export type DecreaseLiquidityInstructionDataArgs = { liquidityAmount: number | bigint; tokenMinA: number | bigint; tokenMinB: number | bigint;  };

export function getDecreaseLiquidityInstructionDataEncoder(): FixedSizeEncoder<DecreaseLiquidityInstructionDataArgs> {
    return transformEncoder(getStructEncoder([['discriminator', fixEncoderSize(getBytesEncoder(), 8)], ['liquidityAmount', getU128Encoder()], ['tokenMinA', getU64Encoder()], ['tokenMinB', getU64Encoder()]]), (value) => ({ ...value, discriminator: DECREASE_LIQUIDITY_DISCRIMINATOR }));
}

export function getDecreaseLiquidityInstructionDataDecoder(): FixedSizeDecoder<DecreaseLiquidityInstructionData> {
    return getStructDecoder([['discriminator', fixDecoderSize(getBytesDecoder(), 8)], ['liquidityAmount', getU128Decoder()], ['tokenMinA', getU64Decoder()], ['tokenMinB', getU64Decoder()]]);
}

export function getDecreaseLiquidityInstructionDataCodec(): FixedSizeCodec<DecreaseLiquidityInstructionDataArgs, DecreaseLiquidityInstructionData> {
    return combineCodec(getDecreaseLiquidityInstructionDataEncoder(), getDecreaseLiquidityInstructionDataDecoder());
}

export type DecreaseLiquidityInput<TAccountWhirlpool extends string = string, TAccountTokenProgram extends string = string, TAccountPositionAuthority extends string = string, TAccountPosition extends string = string, TAccountPositionTokenAccount extends string = string, TAccountTokenOwnerAccountA extends string = string, TAccountTokenOwnerAccountB extends string = string, TAccountTokenVaultA extends string = string, TAccountTokenVaultB extends string = string, TAccountTickArrayLower extends string = string, TAccountTickArrayUpper extends string = string> =  {
  whirlpool: Address<TAccountWhirlpool>;
tokenProgram?: Address<TAccountTokenProgram>;
positionAuthority: TransactionSigner<TAccountPositionAuthority>;
position: Address<TAccountPosition>;
positionTokenAccount: Address<TAccountPositionTokenAccount>;
tokenOwnerAccountA: Address<TAccountTokenOwnerAccountA>;
tokenOwnerAccountB: Address<TAccountTokenOwnerAccountB>;
tokenVaultA: Address<TAccountTokenVaultA>;
tokenVaultB: Address<TAccountTokenVaultB>;
tickArrayLower: Address<TAccountTickArrayLower>;
tickArrayUpper: Address<TAccountTickArrayUpper>;
liquidityAmount: DecreaseLiquidityInstructionDataArgs["liquidityAmount"];
tokenMinA: DecreaseLiquidityInstructionDataArgs["tokenMinA"];
tokenMinB: DecreaseLiquidityInstructionDataArgs["tokenMinB"];
}

export function getDecreaseLiquidityInstruction<TAccountWhirlpool extends string, TAccountTokenProgram extends string, TAccountPositionAuthority extends string, TAccountPosition extends string, TAccountPositionTokenAccount extends string, TAccountTokenOwnerAccountA extends string, TAccountTokenOwnerAccountB extends string, TAccountTokenVaultA extends string, TAccountTokenVaultB extends string, TAccountTickArrayLower extends string, TAccountTickArrayUpper extends string, TProgramAddress extends Address = typeof WHIRLPOOL_PROGRAM_ADDRESS>(input: DecreaseLiquidityInput<TAccountWhirlpool, TAccountTokenProgram, TAccountPositionAuthority, TAccountPosition, TAccountPositionTokenAccount, TAccountTokenOwnerAccountA, TAccountTokenOwnerAccountB, TAccountTokenVaultA, TAccountTokenVaultB, TAccountTickArrayLower, TAccountTickArrayUpper>, config?: { programAddress?: TProgramAddress } ): DecreaseLiquidityInstruction<TProgramAddress, TAccountWhirlpool, TAccountTokenProgram, TAccountPositionAuthority, TAccountPosition, TAccountPositionTokenAccount, TAccountTokenOwnerAccountA, TAccountTokenOwnerAccountB, TAccountTokenVaultA, TAccountTokenVaultB, TAccountTickArrayLower, TAccountTickArrayUpper> {
  // Program address.
const programAddress = config?.programAddress ?? WHIRLPOOL_PROGRAM_ADDRESS;

 // Original accounts.
const originalAccounts = { whirlpool: { value: input.whirlpool ?? null, isWritable: true }, tokenProgram: { value: input.tokenProgram ?? null, isWritable: false }, positionAuthority: { value: input.positionAuthority ?? null, isWritable: false }, position: { value: input.position ?? null, isWritable: true }, positionTokenAccount: { value: input.positionTokenAccount ?? null, isWritable: false }, tokenOwnerAccountA: { value: input.tokenOwnerAccountA ?? null, isWritable: true }, tokenOwnerAccountB: { value: input.tokenOwnerAccountB ?? null, isWritable: true }, tokenVaultA: { value: input.tokenVaultA ?? null, isWritable: true }, tokenVaultB: { value: input.tokenVaultB ?? null, isWritable: true }, tickArrayLower: { value: input.tickArrayLower ?? null, isWritable: true }, tickArrayUpper: { value: input.tickArrayUpper ?? null, isWritable: true } }
const accounts = originalAccounts as Record<keyof typeof originalAccounts, ResolvedAccount>;


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


// Resolve default values.
if (!accounts.tokenProgram.value) {
accounts.tokenProgram.value = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' as Address<'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'>;
}

const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
return Object.freeze({ accounts: [getAccountMeta(accounts.whirlpool), getAccountMeta(accounts.tokenProgram), getAccountMeta(accounts.positionAuthority), getAccountMeta(accounts.position), getAccountMeta(accounts.positionTokenAccount), getAccountMeta(accounts.tokenOwnerAccountA), getAccountMeta(accounts.tokenOwnerAccountB), getAccountMeta(accounts.tokenVaultA), getAccountMeta(accounts.tokenVaultB), getAccountMeta(accounts.tickArrayLower), getAccountMeta(accounts.tickArrayUpper)], data: getDecreaseLiquidityInstructionDataEncoder().encode(args as DecreaseLiquidityInstructionDataArgs), programAddress } as DecreaseLiquidityInstruction<TProgramAddress, TAccountWhirlpool, TAccountTokenProgram, TAccountPositionAuthority, TAccountPosition, TAccountPositionTokenAccount, TAccountTokenOwnerAccountA, TAccountTokenOwnerAccountB, TAccountTokenVaultA, TAccountTokenVaultB, TAccountTickArrayLower, TAccountTickArrayUpper>);
}

export type ParsedDecreaseLiquidityInstruction<TProgram extends string = typeof WHIRLPOOL_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[]> = { programAddress: Address<TProgram>;
accounts: {
whirlpool: TAccountMetas[0];
tokenProgram: TAccountMetas[1];
positionAuthority: TAccountMetas[2];
position: TAccountMetas[3];
positionTokenAccount: TAccountMetas[4];
tokenOwnerAccountA: TAccountMetas[5];
tokenOwnerAccountB: TAccountMetas[6];
tokenVaultA: TAccountMetas[7];
tokenVaultB: TAccountMetas[8];
tickArrayLower: TAccountMetas[9];
tickArrayUpper: TAccountMetas[10];
};
data: DecreaseLiquidityInstructionData; };

export function parseDecreaseLiquidityInstruction<TProgram extends string, TAccountMetas extends readonly AccountMeta[]>(instruction: Instruction<TProgram> & InstructionWithAccounts<TAccountMetas> & InstructionWithData<ReadonlyUint8Array>): ParsedDecreaseLiquidityInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 11) {
  // 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: { whirlpool: getNextAccount(), tokenProgram: getNextAccount(), positionAuthority: getNextAccount(), position: getNextAccount(), positionTokenAccount: getNextAccount(), tokenOwnerAccountA: getNextAccount(), tokenOwnerAccountB: getNextAccount(), tokenVaultA: getNextAccount(), tokenVaultB: getNextAccount(), tickArrayLower: getNextAccount(), tickArrayUpper: getNextAccount() }, data: getDecreaseLiquidityInstructionDataDecoder().decode(instruction.data) };
}