/**
 * 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 {
  type AccountMeta,
  type Address,
  type Instruction,
  type InstructionWithAccounts,
  type WritableAccount,
} from '@solana/kit';
import { DUMMY_PROGRAM_ADDRESS } from '../programs';
import { getAccountMetaFactory, type ResolvedAccount } from '../shared';

export type Instruction6Instruction<
  TProgram extends string = typeof DUMMY_PROGRAM_ADDRESS,
  TAccountMyAccount extends string | AccountMeta<string> = string,
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithAccounts<
    [
      TAccountMyAccount extends string
        ? WritableAccount<TAccountMyAccount>
        : TAccountMyAccount,
      ...TRemainingAccounts,
    ]
  >;

export type Instruction6Input<TAccountMyAccount extends string = string> = {
  myAccount: Address<TAccountMyAccount>;
};

export function getInstruction6Instruction<
  TAccountMyAccount extends string,
  TProgramAddress extends Address = typeof DUMMY_PROGRAM_ADDRESS,
>(
  input: Instruction6Input<TAccountMyAccount>,
  config?: { programAddress?: TProgramAddress }
): Instruction6Instruction<TProgramAddress, TAccountMyAccount> {
  // Program address.
  const programAddress = config?.programAddress ?? DUMMY_PROGRAM_ADDRESS;

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

  const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  const instruction = {
    accounts: [getAccountMeta(accounts.myAccount)],
    programAddress,
  } as Instruction6Instruction<TProgramAddress, TAccountMyAccount>;

  return instruction;
}

export type ParsedInstruction6Instruction<
  TProgram extends string = typeof DUMMY_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    myAccount: TAccountMetas[0];
  };
};

export function parseInstruction6Instruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> & InstructionWithAccounts<TAccountMetas>
): ParsedInstruction6Instruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 1) {
    // 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: {
      myAccount: getNextAccount(),
    },
  };
}
