/**
 * 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, getU16Decoder, getU16Encoder, getU32Decoder, getU32Encoder, transformEncoder, type AccountMeta, type AccountSignerMeta, type Address, type FixedSizeCodec, type FixedSizeDecoder, type FixedSizeEncoder, type Instruction, type InstructionWithAccounts, type InstructionWithData, type ReadonlyAccount, type ReadonlyUint8Array, type TransactionSigner, type WritableAccount, type WritableSignerAccount } from '@solana/kit';
import { AMM_V3_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';

export const CREATE_AMM_CONFIG_DISCRIMINATOR = new Uint8Array([137, 52, 237, 212, 215, 117, 108, 104]);

export function getCreateAmmConfigDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode(CREATE_AMM_CONFIG_DISCRIMINATOR); }

export type CreateAmmConfigInstruction<TProgram extends string = typeof AMM_V3_PROGRAM_ADDRESS, TAccountOwner extends string | AccountMeta<string> = string, TAccountAmmConfig extends string | AccountMeta<string> = string, TAccountSystemProgram extends string | AccountMeta<string> = "11111111111111111111111111111111", TRemainingAccounts extends readonly AccountMeta<string>[] = []> =
Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> & InstructionWithAccounts<[TAccountOwner extends string ? WritableSignerAccount<TAccountOwner> & AccountSignerMeta<TAccountOwner> : TAccountOwner, TAccountAmmConfig extends string ? WritableAccount<TAccountAmmConfig> : TAccountAmmConfig, TAccountSystemProgram extends string ? ReadonlyAccount<TAccountSystemProgram> : TAccountSystemProgram, ...TRemainingAccounts]>;

export type CreateAmmConfigInstructionData = { discriminator: ReadonlyUint8Array; index: number; tickSpacing: number; tradeFeeRate: number; protocolFeeRate: number; fundFeeRate: number;  };

export type CreateAmmConfigInstructionDataArgs = { index: number; tickSpacing: number; tradeFeeRate: number; protocolFeeRate: number; fundFeeRate: number;  };

export function getCreateAmmConfigInstructionDataEncoder(): FixedSizeEncoder<CreateAmmConfigInstructionDataArgs> {
    return transformEncoder(getStructEncoder([['discriminator', fixEncoderSize(getBytesEncoder(), 8)], ['index', getU16Encoder()], ['tickSpacing', getU16Encoder()], ['tradeFeeRate', getU32Encoder()], ['protocolFeeRate', getU32Encoder()], ['fundFeeRate', getU32Encoder()]]), (value) => ({ ...value, discriminator: CREATE_AMM_CONFIG_DISCRIMINATOR }));
}

export function getCreateAmmConfigInstructionDataDecoder(): FixedSizeDecoder<CreateAmmConfigInstructionData> {
    return getStructDecoder([['discriminator', fixDecoderSize(getBytesDecoder(), 8)], ['index', getU16Decoder()], ['tickSpacing', getU16Decoder()], ['tradeFeeRate', getU32Decoder()], ['protocolFeeRate', getU32Decoder()], ['fundFeeRate', getU32Decoder()]]);
}

export function getCreateAmmConfigInstructionDataCodec(): FixedSizeCodec<CreateAmmConfigInstructionDataArgs, CreateAmmConfigInstructionData> {
    return combineCodec(getCreateAmmConfigInstructionDataEncoder(), getCreateAmmConfigInstructionDataDecoder());
}

export type CreateAmmConfigInput<TAccountOwner extends string = string, TAccountAmmConfig extends string = string, TAccountSystemProgram extends string = string> =  {
  owner: TransactionSigner<TAccountOwner>;
ammConfig: Address<TAccountAmmConfig>;
systemProgram?: Address<TAccountSystemProgram>;
index: CreateAmmConfigInstructionDataArgs["index"];
tickSpacing: CreateAmmConfigInstructionDataArgs["tickSpacing"];
tradeFeeRate: CreateAmmConfigInstructionDataArgs["tradeFeeRate"];
protocolFeeRate: CreateAmmConfigInstructionDataArgs["protocolFeeRate"];
fundFeeRate: CreateAmmConfigInstructionDataArgs["fundFeeRate"];
}

export function getCreateAmmConfigInstruction<TAccountOwner extends string, TAccountAmmConfig extends string, TAccountSystemProgram extends string, TProgramAddress extends Address = typeof AMM_V3_PROGRAM_ADDRESS>(input: CreateAmmConfigInput<TAccountOwner, TAccountAmmConfig, TAccountSystemProgram>, config?: { programAddress?: TProgramAddress } ): CreateAmmConfigInstruction<TProgramAddress, TAccountOwner, TAccountAmmConfig, TAccountSystemProgram> {
  // Program address.
const programAddress = config?.programAddress ?? AMM_V3_PROGRAM_ADDRESS;

 // Original accounts.
const originalAccounts = { owner: { value: input.owner ?? null, isWritable: true }, ammConfig: { value: input.ammConfig ?? null, isWritable: true }, systemProgram: { value: input.systemProgram ?? null, isWritable: false } }
const accounts = originalAccounts as Record<keyof typeof originalAccounts, ResolvedAccount>;


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


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

const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
return Object.freeze({ accounts: [getAccountMeta(accounts.owner), getAccountMeta(accounts.ammConfig), getAccountMeta(accounts.systemProgram)], data: getCreateAmmConfigInstructionDataEncoder().encode(args as CreateAmmConfigInstructionDataArgs), programAddress } as CreateAmmConfigInstruction<TProgramAddress, TAccountOwner, TAccountAmmConfig, TAccountSystemProgram>);
}

export type ParsedCreateAmmConfigInstruction<TProgram extends string = typeof AMM_V3_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[]> = { programAddress: Address<TProgram>;
accounts: {
owner: TAccountMetas[0];
ammConfig: TAccountMetas[1];
systemProgram: TAccountMetas[2];
};
data: CreateAmmConfigInstructionData; };

export function parseCreateAmmConfigInstruction<TProgram extends string, TAccountMetas extends readonly AccountMeta[]>(instruction: Instruction<TProgram> & InstructionWithAccounts<TAccountMetas> & InstructionWithData<ReadonlyUint8Array>): ParsedCreateAmmConfigInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 3) {
  // 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: { owner: getNextAccount(), ammConfig: getNextAccount(), systemProgram: getNextAccount() }, data: getCreateAmmConfigInstructionDataDecoder().decode(instruction.data) };
}