/**
 * 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, getAddressDecoder, getAddressEncoder, getBytesDecoder, getBytesEncoder, getStructDecoder, getStructEncoder, getU16Decoder, getU16Encoder, 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 WritableSignerAccount } from '@solana/kit';
import { WHIRLPOOL_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';

export const INITIALIZE_CONFIG_DISCRIMINATOR = new Uint8Array([208, 127, 21, 1, 194, 190, 196, 70]);

export function getInitializeConfigDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode(INITIALIZE_CONFIG_DISCRIMINATOR); }

export type InitializeConfigInstruction<TProgram extends string = typeof WHIRLPOOL_PROGRAM_ADDRESS, TAccountConfig extends string | AccountMeta<string> = string, TAccountFunder extends string | AccountMeta<string> = string, TAccountSystemProgram extends string | AccountMeta<string> = "11111111111111111111111111111111", TRemainingAccounts extends readonly AccountMeta<string>[] = []> =
Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> & InstructionWithAccounts<[TAccountConfig extends string ? WritableSignerAccount<TAccountConfig> & AccountSignerMeta<TAccountConfig> : TAccountConfig, TAccountFunder extends string ? WritableSignerAccount<TAccountFunder> & AccountSignerMeta<TAccountFunder> : TAccountFunder, TAccountSystemProgram extends string ? ReadonlyAccount<TAccountSystemProgram> : TAccountSystemProgram, ...TRemainingAccounts]>;

export type InitializeConfigInstructionData = { discriminator: ReadonlyUint8Array; feeAuthority: Address; collectProtocolFeesAuthority: Address; rewardEmissionsSuperAuthority: Address; defaultProtocolFeeRate: number;  };

export type InitializeConfigInstructionDataArgs = { feeAuthority: Address; collectProtocolFeesAuthority: Address; rewardEmissionsSuperAuthority: Address; defaultProtocolFeeRate: number;  };

export function getInitializeConfigInstructionDataEncoder(): FixedSizeEncoder<InitializeConfigInstructionDataArgs> {
    return transformEncoder(getStructEncoder([['discriminator', fixEncoderSize(getBytesEncoder(), 8)], ['feeAuthority', getAddressEncoder()], ['collectProtocolFeesAuthority', getAddressEncoder()], ['rewardEmissionsSuperAuthority', getAddressEncoder()], ['defaultProtocolFeeRate', getU16Encoder()]]), (value) => ({ ...value, discriminator: INITIALIZE_CONFIG_DISCRIMINATOR }));
}

export function getInitializeConfigInstructionDataDecoder(): FixedSizeDecoder<InitializeConfigInstructionData> {
    return getStructDecoder([['discriminator', fixDecoderSize(getBytesDecoder(), 8)], ['feeAuthority', getAddressDecoder()], ['collectProtocolFeesAuthority', getAddressDecoder()], ['rewardEmissionsSuperAuthority', getAddressDecoder()], ['defaultProtocolFeeRate', getU16Decoder()]]);
}

export function getInitializeConfigInstructionDataCodec(): FixedSizeCodec<InitializeConfigInstructionDataArgs, InitializeConfigInstructionData> {
    return combineCodec(getInitializeConfigInstructionDataEncoder(), getInitializeConfigInstructionDataDecoder());
}

export type InitializeConfigInput<TAccountConfig extends string = string, TAccountFunder extends string = string, TAccountSystemProgram extends string = string> =  {
  config: TransactionSigner<TAccountConfig>;
funder: TransactionSigner<TAccountFunder>;
systemProgram?: Address<TAccountSystemProgram>;
feeAuthority: InitializeConfigInstructionDataArgs["feeAuthority"];
collectProtocolFeesAuthority: InitializeConfigInstructionDataArgs["collectProtocolFeesAuthority"];
rewardEmissionsSuperAuthority: InitializeConfigInstructionDataArgs["rewardEmissionsSuperAuthority"];
defaultProtocolFeeRate: InitializeConfigInstructionDataArgs["defaultProtocolFeeRate"];
}

export function getInitializeConfigInstruction<TAccountConfig extends string, TAccountFunder extends string, TAccountSystemProgram extends string, TProgramAddress extends Address = typeof WHIRLPOOL_PROGRAM_ADDRESS>(input: InitializeConfigInput<TAccountConfig, TAccountFunder, TAccountSystemProgram>, config?: { programAddress?: TProgramAddress } ): InitializeConfigInstruction<TProgramAddress, TAccountConfig, TAccountFunder, TAccountSystemProgram> {
  // Program address.
const programAddress = config?.programAddress ?? WHIRLPOOL_PROGRAM_ADDRESS;

 // Original accounts.
const originalAccounts = { config: { value: input.config ?? null, isWritable: true }, funder: { value: input.funder ?? 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.config), getAccountMeta(accounts.funder), getAccountMeta(accounts.systemProgram)], data: getInitializeConfigInstructionDataEncoder().encode(args as InitializeConfigInstructionDataArgs), programAddress } as InitializeConfigInstruction<TProgramAddress, TAccountConfig, TAccountFunder, TAccountSystemProgram>);
}

export type ParsedInitializeConfigInstruction<TProgram extends string = typeof WHIRLPOOL_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[]> = { programAddress: Address<TProgram>;
accounts: {
config: TAccountMetas[0];
funder: TAccountMetas[1];
systemProgram: TAccountMetas[2];
};
data: InitializeConfigInstructionData; };

export function parseInitializeConfigInstruction<TProgram extends string, TAccountMetas extends readonly AccountMeta[]>(instruction: Instruction<TProgram> & InstructionWithAccounts<TAccountMetas> & InstructionWithData<ReadonlyUint8Array>): ParsedInitializeConfigInstruction<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: { config: getNextAccount(), funder: getNextAccount(), systemProgram: getNextAccount() }, data: getInitializeConfigInstructionDataDecoder().decode(instruction.data) };
}