/**
 * This code was AUTOGENERATED using the kinobi library.
 * Please DO NOT EDIT THIS FILE, instead use visitors
 * to add features, then rerun kinobi to update it.
 *
 * @see https://github.com/kinobi-so/kinobi
 */

import {
  combineCodec,
  getAddressDecoder,
  getAddressEncoder,
  getStructDecoder,
  getStructEncoder,
  getU32Decoder,
  getU32Encoder,
  transformEncoder,
  type Address,
  type Codec,
  type Decoder,
  type Encoder,
  type IAccountMeta,
  type IAccountSignerMeta,
  type IInstruction,
  type IInstructionWithAccounts,
  type IInstructionWithData,
  type TransactionSigner,
  type WritableSignerAccount,
} from '@solana/web3.js';
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 | IAccountMeta<string> = string,
  TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
> = IInstruction<TProgram> &
  IInstructionWithData<Uint8Array> &
  IInstructionWithAccounts<
    [
      TAccountAccount extends string
        ? WritableSignerAccount<TAccountAccount> &
            IAccountSignerMeta<TAccountAccount>
        : TAccountAccount,
      ...TRemainingAccounts,
    ]
  >;

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

export type AssignInstructionDataArgs = { programAddress: Address };

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

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

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

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 IAccountMeta[] = readonly IAccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    account: TAccountMetas[0];
  };
  data: AssignInstructionData;
};

export function parseAssignInstruction<
  TProgram extends string,
  TAccountMetas extends readonly IAccountMeta[],
>(
  instruction: IInstruction<TProgram> &
    IInstructionWithAccounts<TAccountMetas> &
    IInstructionWithData<Uint8Array>
): 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),
  };
}
