/**
 * 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, getBooleanDecoder, getBooleanEncoder, 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 SWAP_DISCRIMINATOR = new Uint8Array([248, 198, 158, 145, 225, 117, 135, 200]);

export function getSwapDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode(SWAP_DISCRIMINATOR); }

export type SwapInstruction<TProgram extends string = typeof WHIRLPOOL_PROGRAM_ADDRESS, TAccountTokenProgram extends string | AccountMeta<string> = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", TAccountTokenAuthority extends string | AccountMeta<string> = string, TAccountWhirlpool extends string | AccountMeta<string> = string, TAccountTokenOwnerAccountA extends string | AccountMeta<string> = string, TAccountTokenVaultA extends string | AccountMeta<string> = string, TAccountTokenOwnerAccountB extends string | AccountMeta<string> = string, TAccountTokenVaultB extends string | AccountMeta<string> = string, TAccountTickArray0 extends string | AccountMeta<string> = string, TAccountTickArray1 extends string | AccountMeta<string> = string, TAccountTickArray2 extends string | AccountMeta<string> = string, TAccountOracle extends string | AccountMeta<string> = string, TRemainingAccounts extends readonly AccountMeta<string>[] = []> =
Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> & InstructionWithAccounts<[TAccountTokenProgram extends string ? ReadonlyAccount<TAccountTokenProgram> : TAccountTokenProgram, TAccountTokenAuthority extends string ? ReadonlySignerAccount<TAccountTokenAuthority> & AccountSignerMeta<TAccountTokenAuthority> : TAccountTokenAuthority, TAccountWhirlpool extends string ? WritableAccount<TAccountWhirlpool> : TAccountWhirlpool, TAccountTokenOwnerAccountA extends string ? WritableAccount<TAccountTokenOwnerAccountA> : TAccountTokenOwnerAccountA, TAccountTokenVaultA extends string ? WritableAccount<TAccountTokenVaultA> : TAccountTokenVaultA, TAccountTokenOwnerAccountB extends string ? WritableAccount<TAccountTokenOwnerAccountB> : TAccountTokenOwnerAccountB, TAccountTokenVaultB extends string ? WritableAccount<TAccountTokenVaultB> : TAccountTokenVaultB, TAccountTickArray0 extends string ? WritableAccount<TAccountTickArray0> : TAccountTickArray0, TAccountTickArray1 extends string ? WritableAccount<TAccountTickArray1> : TAccountTickArray1, TAccountTickArray2 extends string ? WritableAccount<TAccountTickArray2> : TAccountTickArray2, TAccountOracle extends string ? ReadonlyAccount<TAccountOracle> : TAccountOracle, ...TRemainingAccounts]>;

export type SwapInstructionData = { discriminator: ReadonlyUint8Array; amount: bigint; otherAmountThreshold: bigint; sqrtPriceLimit: bigint; amountSpecifiedIsInput: boolean; aToB: boolean;  };

export type SwapInstructionDataArgs = { amount: number | bigint; otherAmountThreshold: number | bigint; sqrtPriceLimit: number | bigint; amountSpecifiedIsInput: boolean; aToB: boolean;  };

export function getSwapInstructionDataEncoder(): FixedSizeEncoder<SwapInstructionDataArgs> {
    return transformEncoder(getStructEncoder([['discriminator', fixEncoderSize(getBytesEncoder(), 8)], ['amount', getU64Encoder()], ['otherAmountThreshold', getU64Encoder()], ['sqrtPriceLimit', getU128Encoder()], ['amountSpecifiedIsInput', getBooleanEncoder()], ['aToB', getBooleanEncoder()]]), (value) => ({ ...value, discriminator: SWAP_DISCRIMINATOR }));
}

export function getSwapInstructionDataDecoder(): FixedSizeDecoder<SwapInstructionData> {
    return getStructDecoder([['discriminator', fixDecoderSize(getBytesDecoder(), 8)], ['amount', getU64Decoder()], ['otherAmountThreshold', getU64Decoder()], ['sqrtPriceLimit', getU128Decoder()], ['amountSpecifiedIsInput', getBooleanDecoder()], ['aToB', getBooleanDecoder()]]);
}

export function getSwapInstructionDataCodec(): FixedSizeCodec<SwapInstructionDataArgs, SwapInstructionData> {
    return combineCodec(getSwapInstructionDataEncoder(), getSwapInstructionDataDecoder());
}

export type SwapInput<TAccountTokenProgram extends string = string, TAccountTokenAuthority extends string = string, TAccountWhirlpool extends string = string, TAccountTokenOwnerAccountA extends string = string, TAccountTokenVaultA extends string = string, TAccountTokenOwnerAccountB extends string = string, TAccountTokenVaultB extends string = string, TAccountTickArray0 extends string = string, TAccountTickArray1 extends string = string, TAccountTickArray2 extends string = string, TAccountOracle extends string = string> =  {
  tokenProgram?: Address<TAccountTokenProgram>;
tokenAuthority: TransactionSigner<TAccountTokenAuthority>;
whirlpool: Address<TAccountWhirlpool>;
tokenOwnerAccountA: Address<TAccountTokenOwnerAccountA>;
tokenVaultA: Address<TAccountTokenVaultA>;
tokenOwnerAccountB: Address<TAccountTokenOwnerAccountB>;
tokenVaultB: Address<TAccountTokenVaultB>;
tickArray0: Address<TAccountTickArray0>;
tickArray1: Address<TAccountTickArray1>;
tickArray2: Address<TAccountTickArray2>;
oracle: Address<TAccountOracle>;
amount: SwapInstructionDataArgs["amount"];
otherAmountThreshold: SwapInstructionDataArgs["otherAmountThreshold"];
sqrtPriceLimit: SwapInstructionDataArgs["sqrtPriceLimit"];
amountSpecifiedIsInput: SwapInstructionDataArgs["amountSpecifiedIsInput"];
aToB: SwapInstructionDataArgs["aToB"];
}

export function getSwapInstruction<TAccountTokenProgram extends string, TAccountTokenAuthority extends string, TAccountWhirlpool extends string, TAccountTokenOwnerAccountA extends string, TAccountTokenVaultA extends string, TAccountTokenOwnerAccountB extends string, TAccountTokenVaultB extends string, TAccountTickArray0 extends string, TAccountTickArray1 extends string, TAccountTickArray2 extends string, TAccountOracle extends string, TProgramAddress extends Address = typeof WHIRLPOOL_PROGRAM_ADDRESS>(input: SwapInput<TAccountTokenProgram, TAccountTokenAuthority, TAccountWhirlpool, TAccountTokenOwnerAccountA, TAccountTokenVaultA, TAccountTokenOwnerAccountB, TAccountTokenVaultB, TAccountTickArray0, TAccountTickArray1, TAccountTickArray2, TAccountOracle>, config?: { programAddress?: TProgramAddress } ): SwapInstruction<TProgramAddress, TAccountTokenProgram, TAccountTokenAuthority, TAccountWhirlpool, TAccountTokenOwnerAccountA, TAccountTokenVaultA, TAccountTokenOwnerAccountB, TAccountTokenVaultB, TAccountTickArray0, TAccountTickArray1, TAccountTickArray2, TAccountOracle> {
  // Program address.
const programAddress = config?.programAddress ?? WHIRLPOOL_PROGRAM_ADDRESS;

 // Original accounts.
const originalAccounts = { tokenProgram: { value: input.tokenProgram ?? null, isWritable: false }, tokenAuthority: { value: input.tokenAuthority ?? null, isWritable: false }, whirlpool: { value: input.whirlpool ?? null, isWritable: true }, tokenOwnerAccountA: { value: input.tokenOwnerAccountA ?? null, isWritable: true }, tokenVaultA: { value: input.tokenVaultA ?? null, isWritable: true }, tokenOwnerAccountB: { value: input.tokenOwnerAccountB ?? null, isWritable: true }, tokenVaultB: { value: input.tokenVaultB ?? null, isWritable: true }, tickArray0: { value: input.tickArray0 ?? null, isWritable: true }, tickArray1: { value: input.tickArray1 ?? null, isWritable: true }, tickArray2: { value: input.tickArray2 ?? null, isWritable: true }, oracle: { value: input.oracle ?? null, isWritable: false } }
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.tokenProgram), getAccountMeta(accounts.tokenAuthority), getAccountMeta(accounts.whirlpool), getAccountMeta(accounts.tokenOwnerAccountA), getAccountMeta(accounts.tokenVaultA), getAccountMeta(accounts.tokenOwnerAccountB), getAccountMeta(accounts.tokenVaultB), getAccountMeta(accounts.tickArray0), getAccountMeta(accounts.tickArray1), getAccountMeta(accounts.tickArray2), getAccountMeta(accounts.oracle)], data: getSwapInstructionDataEncoder().encode(args as SwapInstructionDataArgs), programAddress } as SwapInstruction<TProgramAddress, TAccountTokenProgram, TAccountTokenAuthority, TAccountWhirlpool, TAccountTokenOwnerAccountA, TAccountTokenVaultA, TAccountTokenOwnerAccountB, TAccountTokenVaultB, TAccountTickArray0, TAccountTickArray1, TAccountTickArray2, TAccountOracle>);
}

export type ParsedSwapInstruction<TProgram extends string = typeof WHIRLPOOL_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[]> = { programAddress: Address<TProgram>;
accounts: {
tokenProgram: TAccountMetas[0];
tokenAuthority: TAccountMetas[1];
whirlpool: TAccountMetas[2];
tokenOwnerAccountA: TAccountMetas[3];
tokenVaultA: TAccountMetas[4];
tokenOwnerAccountB: TAccountMetas[5];
tokenVaultB: TAccountMetas[6];
tickArray0: TAccountMetas[7];
tickArray1: TAccountMetas[8];
tickArray2: TAccountMetas[9];
oracle: TAccountMetas[10];
};
data: SwapInstructionData; };

export function parseSwapInstruction<TProgram extends string, TAccountMetas extends readonly AccountMeta[]>(instruction: Instruction<TProgram> & InstructionWithAccounts<TAccountMetas> & InstructionWithData<ReadonlyUint8Array>): ParsedSwapInstruction<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: { tokenProgram: getNextAccount(), tokenAuthority: getNextAccount(), whirlpool: getNextAccount(), tokenOwnerAccountA: getNextAccount(), tokenVaultA: getNextAccount(), tokenOwnerAccountB: getNextAccount(), tokenVaultB: getNextAccount(), tickArray0: getNextAccount(), tickArray1: getNextAccount(), tickArray2: getNextAccount(), oracle: getNextAccount() }, data: getSwapInstructionDataDecoder().decode(instruction.data) };
}