/**
 * 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,
  getStructDecoder,
  getStructEncoder,
  getU32Decoder,
  getU32Encoder,
  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 { SYSTEM_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';

export const ADVANCE_NONCE_ACCOUNT_DISCRIMINATOR = 4;

export function getAdvanceNonceAccountDiscriminatorBytes() {
  return getU32Encoder().encode(ADVANCE_NONCE_ACCOUNT_DISCRIMINATOR);
}

export type AdvanceNonceAccountInstruction<
  TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS,
  TAccountNonceAccount extends string | AccountMeta<string> = string,
  TAccountRecentBlockhashesSysvar extends
    | string
    | AccountMeta<string> = 'SysvarRecentB1ockHashes11111111111111111111',
  TAccountNonceAuthority extends string | AccountMeta<string> = string,
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountNonceAccount extends string
        ? WritableAccount<TAccountNonceAccount>
        : TAccountNonceAccount,
      TAccountRecentBlockhashesSysvar extends string
        ? ReadonlyAccount<TAccountRecentBlockhashesSysvar>
        : TAccountRecentBlockhashesSysvar,
      TAccountNonceAuthority extends string
        ? ReadonlySignerAccount<TAccountNonceAuthority> &
            AccountSignerMeta<TAccountNonceAuthority>
        : TAccountNonceAuthority,
      ...TRemainingAccounts,
    ]
  >;

export type AdvanceNonceAccountInstructionData = { discriminator: number };

export type AdvanceNonceAccountInstructionDataArgs = {};

export function getAdvanceNonceAccountInstructionDataEncoder(): FixedSizeEncoder<AdvanceNonceAccountInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([['discriminator', getU32Encoder()]]),
    (value) => ({
      ...value,
      discriminator: ADVANCE_NONCE_ACCOUNT_DISCRIMINATOR,
    })
  ) as FixedSizeEncoder<AdvanceNonceAccountInstructionDataArgs>;
}

export function getAdvanceNonceAccountInstructionDataDecoder(): FixedSizeDecoder<AdvanceNonceAccountInstructionData> {
  return getStructDecoder([
    ['discriminator', getU32Decoder()],
  ]) as FixedSizeDecoder<AdvanceNonceAccountInstructionData>;
}

export function getAdvanceNonceAccountInstructionDataCodec(): FixedSizeCodec<
  AdvanceNonceAccountInstructionDataArgs,
  AdvanceNonceAccountInstructionData
> {
  return combineCodec(
    getAdvanceNonceAccountInstructionDataEncoder(),
    getAdvanceNonceAccountInstructionDataDecoder()
  ) as FixedSizeCodec<
    AdvanceNonceAccountInstructionDataArgs,
    AdvanceNonceAccountInstructionData
  >;
}

export type AdvanceNonceAccountInput<
  TAccountNonceAccount extends string = string,
  TAccountRecentBlockhashesSysvar extends string = string,
  TAccountNonceAuthority extends string = string,
> = {
  nonceAccount: Address<TAccountNonceAccount>;
  recentBlockhashesSysvar?: Address<TAccountRecentBlockhashesSysvar>;
  nonceAuthority: TransactionSigner<TAccountNonceAuthority>;
};

export function getAdvanceNonceAccountInstruction<
  TAccountNonceAccount extends string,
  TAccountRecentBlockhashesSysvar extends string,
  TAccountNonceAuthority extends string,
  TProgramAddress extends Address = typeof SYSTEM_PROGRAM_ADDRESS,
>(
  input: AdvanceNonceAccountInput<
    TAccountNonceAccount,
    TAccountRecentBlockhashesSysvar,
    TAccountNonceAuthority
  >,
  config?: { programAddress?: TProgramAddress }
): AdvanceNonceAccountInstruction<
  TProgramAddress,
  TAccountNonceAccount,
  TAccountRecentBlockhashesSysvar,
  TAccountNonceAuthority
> {
  // Program address.
  const programAddress = config?.programAddress ?? SYSTEM_PROGRAM_ADDRESS;

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

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

  const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  const instruction = {
    accounts: [
      getAccountMeta(accounts.nonceAccount),
      getAccountMeta(accounts.recentBlockhashesSysvar),
      getAccountMeta(accounts.nonceAuthority),
    ],
    programAddress,
    data: getAdvanceNonceAccountInstructionDataEncoder().encode({}),
  } as AdvanceNonceAccountInstruction<
    TProgramAddress,
    TAccountNonceAccount,
    TAccountRecentBlockhashesSysvar,
    TAccountNonceAuthority
  >;

  return instruction;
}

export type ParsedAdvanceNonceAccountInstruction<
  TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    nonceAccount: TAccountMetas[0];
    recentBlockhashesSysvar: TAccountMetas[1];
    nonceAuthority: TAccountMetas[2];
  };
  data: AdvanceNonceAccountInstructionData;
};

export function parseAdvanceNonceAccountInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>
): ParsedAdvanceNonceAccountInstruction<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![accountIndex]!;
    accountIndex += 1;
    return accountMeta;
  };
  return {
    programAddress: instruction.programAddress,
    accounts: {
      nonceAccount: getNextAccount(),
      recentBlockhashesSysvar: getNextAccount(),
      nonceAuthority: getNextAccount(),
    },
    data: getAdvanceNonceAccountInstructionDataDecoder().decode(
      instruction.data
    ),
  };
}
