/**
 * 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, 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 { YVAULTS_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';

export const UPDATE_GLOBAL_CONFIG_DISCRIMINATOR = new Uint8Array([164, 84, 130, 189, 111, 58, 250, 200]);

export function getUpdateGlobalConfigDiscriminatorBytes() { return fixEncoderSize(getBytesEncoder(), 8).encode(UPDATE_GLOBAL_CONFIG_DISCRIMINATOR); }

export type UpdateGlobalConfigInstruction<TProgram extends string = typeof YVAULTS_PROGRAM_ADDRESS, TAccountAdminAuthority extends string | AccountMeta<string> = string, TAccountGlobalConfig extends string | AccountMeta<string> = string, TAccountSystemProgram extends string | AccountMeta<string> = "11111111111111111111111111111111", TRemainingAccounts extends readonly AccountMeta<string>[] = []> =
Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> & InstructionWithAccounts<[TAccountAdminAuthority extends string ? ReadonlySignerAccount<TAccountAdminAuthority> & AccountSignerMeta<TAccountAdminAuthority> : TAccountAdminAuthority, TAccountGlobalConfig extends string ? WritableAccount<TAccountGlobalConfig> : TAccountGlobalConfig, TAccountSystemProgram extends string ? ReadonlyAccount<TAccountSystemProgram> : TAccountSystemProgram, ...TRemainingAccounts]>;

export type UpdateGlobalConfigInstructionData = { discriminator: ReadonlyUint8Array; key: number; index: number; value: ReadonlyUint8Array;  };

export type UpdateGlobalConfigInstructionDataArgs = { key: number; index: number; value: ReadonlyUint8Array;  };

export function getUpdateGlobalConfigInstructionDataEncoder(): FixedSizeEncoder<UpdateGlobalConfigInstructionDataArgs> {
    return transformEncoder(getStructEncoder([['discriminator', fixEncoderSize(getBytesEncoder(), 8)], ['key', getU16Encoder()], ['index', getU16Encoder()], ['value', fixEncoderSize(getBytesEncoder(), 32)]]), (value) => ({ ...value, discriminator: UPDATE_GLOBAL_CONFIG_DISCRIMINATOR }));
}

export function getUpdateGlobalConfigInstructionDataDecoder(): FixedSizeDecoder<UpdateGlobalConfigInstructionData> {
    return getStructDecoder([['discriminator', fixDecoderSize(getBytesDecoder(), 8)], ['key', getU16Decoder()], ['index', getU16Decoder()], ['value', fixDecoderSize(getBytesDecoder(), 32)]]);
}

export function getUpdateGlobalConfigInstructionDataCodec(): FixedSizeCodec<UpdateGlobalConfigInstructionDataArgs, UpdateGlobalConfigInstructionData> {
    return combineCodec(getUpdateGlobalConfigInstructionDataEncoder(), getUpdateGlobalConfigInstructionDataDecoder());
}

export type UpdateGlobalConfigInput<TAccountAdminAuthority extends string = string, TAccountGlobalConfig extends string = string, TAccountSystemProgram extends string = string> =  {
  adminAuthority: TransactionSigner<TAccountAdminAuthority>;
globalConfig: Address<TAccountGlobalConfig>;
systemProgram?: Address<TAccountSystemProgram>;
key: UpdateGlobalConfigInstructionDataArgs["key"];
index: UpdateGlobalConfigInstructionDataArgs["index"];
value: UpdateGlobalConfigInstructionDataArgs["value"];
}

export function getUpdateGlobalConfigInstruction<TAccountAdminAuthority extends string, TAccountGlobalConfig extends string, TAccountSystemProgram extends string, TProgramAddress extends Address = typeof YVAULTS_PROGRAM_ADDRESS>(input: UpdateGlobalConfigInput<TAccountAdminAuthority, TAccountGlobalConfig, TAccountSystemProgram>, config?: { programAddress?: TProgramAddress } ): UpdateGlobalConfigInstruction<TProgramAddress, TAccountAdminAuthority, TAccountGlobalConfig, TAccountSystemProgram> {
  // Program address.
const programAddress = config?.programAddress ?? YVAULTS_PROGRAM_ADDRESS;

 // Original accounts.
const originalAccounts = { adminAuthority: { value: input.adminAuthority ?? null, isWritable: false }, globalConfig: { value: input.globalConfig ?? 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.adminAuthority), getAccountMeta(accounts.globalConfig), getAccountMeta(accounts.systemProgram)], data: getUpdateGlobalConfigInstructionDataEncoder().encode(args as UpdateGlobalConfigInstructionDataArgs), programAddress } as UpdateGlobalConfigInstruction<TProgramAddress, TAccountAdminAuthority, TAccountGlobalConfig, TAccountSystemProgram>);
}

export type ParsedUpdateGlobalConfigInstruction<TProgram extends string = typeof YVAULTS_PROGRAM_ADDRESS, TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[]> = { programAddress: Address<TProgram>;
accounts: {
adminAuthority: TAccountMetas[0];
globalConfig: TAccountMetas[1];
systemProgram: TAccountMetas[2];
};
data: UpdateGlobalConfigInstructionData; };

export function parseUpdateGlobalConfigInstruction<TProgram extends string, TAccountMetas extends readonly AccountMeta[]>(instruction: Instruction<TProgram> & InstructionWithAccounts<TAccountMetas> & InstructionWithData<ReadonlyUint8Array>): ParsedUpdateGlobalConfigInstruction<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: { adminAuthority: getNextAccount(), globalConfig: getNextAccount(), systemProgram: getNextAccount() }, data: getUpdateGlobalConfigInstructionDataDecoder().decode(instruction.data) };
}