/**
 * 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 {
  addDecoderSizePrefix,
  addEncoderSizePrefix,
  combineCodec,
  getAddressDecoder,
  getAddressEncoder,
  getStructDecoder,
  getStructEncoder,
  getU32Decoder,
  getU32Encoder,
  getUtf8Decoder,
  getUtf8Encoder,
  transformEncoder,
  type AccountMeta,
  type AccountSignerMeta,
  type Address,
  type Codec,
  type Decoder,
  type Encoder,
  type Instruction,
  type InstructionWithAccounts,
  type InstructionWithData,
  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 ASSIGN_WITH_SEED_DISCRIMINATOR = 10;

export function getAssignWithSeedDiscriminatorBytes() {
  return getU32Encoder().encode(ASSIGN_WITH_SEED_DISCRIMINATOR);
}

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

export type AssignWithSeedInstructionData = {
  discriminator: number;
  base: Address;
  seed: string;
  programAddress: Address;
};

export type AssignWithSeedInstructionDataArgs = {
  base: Address;
  seed: string;
  programAddress: Address;
};

export function getAssignWithSeedInstructionDataEncoder(): Encoder<AssignWithSeedInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ['discriminator', getU32Encoder()],
      ['base', getAddressEncoder()],
      ['seed', addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder())],
      ['programAddress', getAddressEncoder()],
    ]),
    (value) => ({ ...value, discriminator: ASSIGN_WITH_SEED_DISCRIMINATOR })
  );
}

export function getAssignWithSeedInstructionDataDecoder(): Decoder<AssignWithSeedInstructionData> {
  return getStructDecoder([
    ['discriminator', getU32Decoder()],
    ['base', getAddressDecoder()],
    ['seed', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())],
    ['programAddress', getAddressDecoder()],
  ]);
}

export function getAssignWithSeedInstructionDataCodec(): Codec<
  AssignWithSeedInstructionDataArgs,
  AssignWithSeedInstructionData
> {
  return combineCodec(
    getAssignWithSeedInstructionDataEncoder(),
    getAssignWithSeedInstructionDataDecoder()
  );
}

export type AssignWithSeedInput<
  TAccountAccount extends string = string,
  TAccountBaseAccount extends string = string,
> = {
  account: Address<TAccountAccount>;
  baseAccount: TransactionSigner<TAccountBaseAccount>;
  base: AssignWithSeedInstructionDataArgs['base'];
  seed: AssignWithSeedInstructionDataArgs['seed'];
  programAddress: AssignWithSeedInstructionDataArgs['programAddress'];
};

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

  // Original accounts.
  const originalAccounts = {
    account: { value: input.account ?? null, isWritable: true },
    baseAccount: { value: input.baseAccount ?? null, isWritable: false },
  };
  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),
      getAccountMeta(accounts.baseAccount),
    ],
    programAddress,
    data: getAssignWithSeedInstructionDataEncoder().encode(
      args as AssignWithSeedInstructionDataArgs
    ),
  } as AssignWithSeedInstruction<
    TProgramAddress,
    TAccountAccount,
    TAccountBaseAccount
  >;

  return instruction;
}

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

export function parseAssignWithSeedInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>
): ParsedAssignWithSeedInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 2) {
    // 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(),
      baseAccount: getNextAccount(),
    },
    data: getAssignWithSeedInstructionDataDecoder().decode(instruction.data),
  };
}
