/**
 * 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,
  fixDecoderSize,
  fixEncoderSize,
  getBytesDecoder,
  getBytesEncoder,
  getStructDecoder,
  getStructEncoder,
  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 { FARMS_PROGRAM_ADDRESS } from "../programs";
import { getAccountMetaFactory, type ResolvedAccount } from "../shared";

export const CLOSE_EMPTY_USER_STATE_DISCRIMINATOR = new Uint8Array([
  240, 24, 9, 227, 86, 225, 199, 95,
]);

export function getCloseEmptyUserStateDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    CLOSE_EMPTY_USER_STATE_DISCRIMINATOR,
  );
}

export type CloseEmptyUserStateInstruction<
  TProgram extends string = typeof FARMS_PROGRAM_ADDRESS,
  TAccountSigner extends string | AccountMeta<string> = string,
  TAccountUserState extends string | AccountMeta<string> = string,
  TAccountFarmState extends string | AccountMeta<string> = string,
  TAccountRentReceiver extends string | AccountMeta<string> = string,
  TAccountSystemProgram extends string | AccountMeta<string> =
    "11111111111111111111111111111111",
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountSigner extends string
        ? ReadonlySignerAccount<TAccountSigner> &
            AccountSignerMeta<TAccountSigner>
        : TAccountSigner,
      TAccountUserState extends string
        ? WritableAccount<TAccountUserState>
        : TAccountUserState,
      TAccountFarmState extends string
        ? ReadonlyAccount<TAccountFarmState>
        : TAccountFarmState,
      TAccountRentReceiver extends string
        ? WritableAccount<TAccountRentReceiver>
        : TAccountRentReceiver,
      TAccountSystemProgram extends string
        ? ReadonlyAccount<TAccountSystemProgram>
        : TAccountSystemProgram,
      ...TRemainingAccounts,
    ]
  >;

export type CloseEmptyUserStateInstructionData = {
  discriminator: ReadonlyUint8Array;
};

export type CloseEmptyUserStateInstructionDataArgs = {};

export function getCloseEmptyUserStateInstructionDataEncoder(): FixedSizeEncoder<CloseEmptyUserStateInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([["discriminator", fixEncoderSize(getBytesEncoder(), 8)]]),
    (value) => ({
      ...value,
      discriminator: CLOSE_EMPTY_USER_STATE_DISCRIMINATOR,
    }),
  );
}

export function getCloseEmptyUserStateInstructionDataDecoder(): FixedSizeDecoder<CloseEmptyUserStateInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
  ]);
}

export function getCloseEmptyUserStateInstructionDataCodec(): FixedSizeCodec<
  CloseEmptyUserStateInstructionDataArgs,
  CloseEmptyUserStateInstructionData
> {
  return combineCodec(
    getCloseEmptyUserStateInstructionDataEncoder(),
    getCloseEmptyUserStateInstructionDataDecoder(),
  );
}

export type CloseEmptyUserStateInput<
  TAccountSigner extends string = string,
  TAccountUserState extends string = string,
  TAccountFarmState extends string = string,
  TAccountRentReceiver extends string = string,
  TAccountSystemProgram extends string = string,
> = {
  /**
   * The account that signs the transaction
   * - Non-delegated: user signs
   * - Delegated: delegated authority signs
   */
  signer: TransactionSigner<TAccountSigner>;
  userState: Address<TAccountUserState>;
  /** The farm state account for validation */
  farmState: Address<TAccountFarmState>;
  /**
   * The account that receives the rent
   * - Non-delegated: user receives the rent
   * - Delegated: farm admin receives the rent
   */
  rentReceiver: Address<TAccountRentReceiver>;
  systemProgram?: Address<TAccountSystemProgram>;
};

export function getCloseEmptyUserStateInstruction<
  TAccountSigner extends string,
  TAccountUserState extends string,
  TAccountFarmState extends string,
  TAccountRentReceiver extends string,
  TAccountSystemProgram extends string,
  TProgramAddress extends Address = typeof FARMS_PROGRAM_ADDRESS,
>(
  input: CloseEmptyUserStateInput<
    TAccountSigner,
    TAccountUserState,
    TAccountFarmState,
    TAccountRentReceiver,
    TAccountSystemProgram
  >,
  config?: { programAddress?: TProgramAddress },
): CloseEmptyUserStateInstruction<
  TProgramAddress,
  TAccountSigner,
  TAccountUserState,
  TAccountFarmState,
  TAccountRentReceiver,
  TAccountSystemProgram
> {
  // Program address.
  const programAddress = config?.programAddress ?? FARMS_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    signer: { value: input.signer ?? null, isWritable: false },
    userState: { value: input.userState ?? null, isWritable: true },
    farmState: { value: input.farmState ?? null, isWritable: false },
    rentReceiver: { value: input.rentReceiver ?? null, isWritable: true },
    systemProgram: { value: input.systemProgram ?? null, isWritable: false },
  };
  const accounts = originalAccounts as Record<
    keyof typeof originalAccounts,
    ResolvedAccount
  >;

  // Resolve default values.
  if (!accounts.systemProgram.value) {
    accounts.systemProgram.value =
      "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.signer),
      getAccountMeta(accounts.userState),
      getAccountMeta(accounts.farmState),
      getAccountMeta(accounts.rentReceiver),
      getAccountMeta(accounts.systemProgram),
    ],
    data: getCloseEmptyUserStateInstructionDataEncoder().encode({}),
    programAddress,
  } as CloseEmptyUserStateInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountUserState,
    TAccountFarmState,
    TAccountRentReceiver,
    TAccountSystemProgram
  >);
}

export type ParsedCloseEmptyUserStateInstruction<
  TProgram extends string = typeof FARMS_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    /**
     * The account that signs the transaction
     * - Non-delegated: user signs
     * - Delegated: delegated authority signs
     */
    signer: TAccountMetas[0];
    userState: TAccountMetas[1];
    /** The farm state account for validation */
    farmState: TAccountMetas[2];
    /**
     * The account that receives the rent
     * - Non-delegated: user receives the rent
     * - Delegated: farm admin receives the rent
     */
    rentReceiver: TAccountMetas[3];
    systemProgram: TAccountMetas[4];
  };
  data: CloseEmptyUserStateInstructionData;
};

export function parseCloseEmptyUserStateInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedCloseEmptyUserStateInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 5) {
    // TODO: Coded error.
    throw new Error("Not enough accounts");
  }
  let accountIndex = 0;
  const getNextAccount = () => {
    const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
    accountIndex += 1;
    return accountMeta;
  };
  return {
    programAddress: instruction.programAddress,
    accounts: {
      signer: getNextAccount(),
      userState: getNextAccount(),
      farmState: getNextAccount(),
      rentReceiver: getNextAccount(),
      systemProgram: getNextAccount(),
    },
    data: getCloseEmptyUserStateInstructionDataDecoder().decode(
      instruction.data,
    ),
  };
}
