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

export const ASSIGN_DISCRIMINATOR = 1;

export function getAssignDiscriminatorBytes() {
  return getU32Encoder().encode(ASSIGN_DISCRIMINATOR);
}

export type AssignInstruction<
  TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS,
  TAccountAccount extends string | AccountMeta<string> = string,
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountAccount extends string
        ? WritableSignerAccount<TAccountAccount> &
            AccountSignerMeta<TAccountAccount>
        : TAccountAccount,
      ...TRemainingAccounts,
    ]
  >;

export type AssignInstructionData = {
  discriminator: number;
  programAddress: Address;
};

export type AssignInstructionDataArgs = { programAddress: Address };

export function getAssignInstructionDataEncoder(): FixedSizeEncoder<AssignInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ['discriminator', getU32Encoder()],
      ['programAddress', getAddressEncoder()],
    ]),
    (value) => ({ ...value, discriminator: ASSIGN_DISCRIMINATOR })
  ) as FixedSizeEncoder<AssignInstructionDataArgs>;
}

export function getAssignInstructionDataDecoder(): FixedSizeDecoder<AssignInstructionData> {
  return getStructDecoder([
    ['discriminator', getU32Decoder()],
    ['programAddress', getAddressDecoder()],
  ]) as FixedSizeDecoder<AssignInstructionData>;
}

export function getAssignInstructionDataCodec(): FixedSizeCodec<
  AssignInstructionDataArgs,
  AssignInstructionData
> {
  return combineCodec(
    getAssignInstructionDataEncoder(),
    getAssignInstructionDataDecoder()
  ) as FixedSizeCodec<AssignInstructionDataArgs, AssignInstructionData>;
}

export type AssignInput<TAccountAccount extends string = string> = {
  account: TransactionSigner<TAccountAccount>;
  programAddress: AssignInstructionDataArgs['programAddress'];
};

export function getAssignInstruction<
  TAccountAccount extends string,
  TProgramAddress extends Address = typeof SYSTEM_PROGRAM_ADDRESS,
>(
  input: AssignInput<TAccountAccount>,
  config?: { programAddress?: TProgramAddress }
): AssignInstruction<TProgramAddress, TAccountAccount> {
  // Program address.
  const programAddress = config?.programAddress ?? SYSTEM_PROGRAM_ADDRESS;

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

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

  const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  const instruction = {
    accounts: [getAccountMeta(accounts.account)],
    programAddress,
    data: getAssignInstructionDataEncoder().encode(
      args as AssignInstructionDataArgs
    ),
  } as AssignInstruction<TProgramAddress, TAccountAccount>;

  return instruction;
}

export type ParsedAssignInstruction<
  TProgram extends string = typeof SYSTEM_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    account: TAccountMetas[0];
  };
  data: AssignInstructionData;
};

export function parseAssignInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>
): ParsedAssignInstruction<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: {
      account: getNextAccount(),
    },
    data: getAssignInstructionDataDecoder().decode(instruction.data),
  };
}
