/**
 * 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,
  type WritableSignerAccount,
} from "@solana/kit";
import { FARMS_PROGRAM_ADDRESS } from "../programs";
import { getAccountMetaFactory, type ResolvedAccount } from "../shared";

export const INITIALIZE_USER_DISCRIMINATOR = new Uint8Array([
  111, 17, 185, 250, 60, 122, 38, 254,
]);

export function getInitializeUserDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    INITIALIZE_USER_DISCRIMINATOR,
  );
}

export type InitializeUserInstruction<
  TProgram extends string = typeof FARMS_PROGRAM_ADDRESS,
  TAccountAuthority extends string | AccountMeta<string> = string,
  TAccountPayer extends string | AccountMeta<string> = string,
  TAccountOwner extends string | AccountMeta<string> = string,
  TAccountDelegatee extends string | AccountMeta<string> = string,
  TAccountUserState extends string | AccountMeta<string> = string,
  TAccountFarmState extends string | AccountMeta<string> = string,
  TAccountSystemProgram extends string | AccountMeta<string> =
    "11111111111111111111111111111111",
  TAccountRent extends string | AccountMeta<string> =
    "SysvarRent111111111111111111111111111111111",
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountAuthority extends string
        ? ReadonlySignerAccount<TAccountAuthority> &
            AccountSignerMeta<TAccountAuthority>
        : TAccountAuthority,
      TAccountPayer extends string
        ? WritableSignerAccount<TAccountPayer> &
            AccountSignerMeta<TAccountPayer>
        : TAccountPayer,
      TAccountOwner extends string
        ? ReadonlyAccount<TAccountOwner>
        : TAccountOwner,
      TAccountDelegatee extends string
        ? ReadonlyAccount<TAccountDelegatee>
        : TAccountDelegatee,
      TAccountUserState extends string
        ? WritableAccount<TAccountUserState>
        : TAccountUserState,
      TAccountFarmState extends string
        ? WritableAccount<TAccountFarmState>
        : TAccountFarmState,
      TAccountSystemProgram extends string
        ? ReadonlyAccount<TAccountSystemProgram>
        : TAccountSystemProgram,
      TAccountRent extends string
        ? ReadonlyAccount<TAccountRent>
        : TAccountRent,
      ...TRemainingAccounts,
    ]
  >;

export type InitializeUserInstructionData = {
  discriminator: ReadonlyUint8Array;
};

export type InitializeUserInstructionDataArgs = {};

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

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

export function getInitializeUserInstructionDataCodec(): FixedSizeCodec<
  InitializeUserInstructionDataArgs,
  InitializeUserInstructionData
> {
  return combineCodec(
    getInitializeUserInstructionDataEncoder(),
    getInitializeUserInstructionDataDecoder(),
  );
}

export type InitializeUserInput<
  TAccountAuthority extends string = string,
  TAccountPayer extends string = string,
  TAccountOwner extends string = string,
  TAccountDelegatee extends string = string,
  TAccountUserState extends string = string,
  TAccountFarmState extends string = string,
  TAccountSystemProgram extends string = string,
  TAccountRent extends string = string,
> = {
  authority: TransactionSigner<TAccountAuthority>;
  payer: TransactionSigner<TAccountPayer>;
  owner: Address<TAccountOwner>;
  delegatee: Address<TAccountDelegatee>;
  userState: Address<TAccountUserState>;
  farmState: Address<TAccountFarmState>;
  systemProgram?: Address<TAccountSystemProgram>;
  rent?: Address<TAccountRent>;
};

export function getInitializeUserInstruction<
  TAccountAuthority extends string,
  TAccountPayer extends string,
  TAccountOwner extends string,
  TAccountDelegatee extends string,
  TAccountUserState extends string,
  TAccountFarmState extends string,
  TAccountSystemProgram extends string,
  TAccountRent extends string,
  TProgramAddress extends Address = typeof FARMS_PROGRAM_ADDRESS,
>(
  input: InitializeUserInput<
    TAccountAuthority,
    TAccountPayer,
    TAccountOwner,
    TAccountDelegatee,
    TAccountUserState,
    TAccountFarmState,
    TAccountSystemProgram,
    TAccountRent
  >,
  config?: { programAddress?: TProgramAddress },
): InitializeUserInstruction<
  TProgramAddress,
  TAccountAuthority,
  TAccountPayer,
  TAccountOwner,
  TAccountDelegatee,
  TAccountUserState,
  TAccountFarmState,
  TAccountSystemProgram,
  TAccountRent
> {
  // Program address.
  const programAddress = config?.programAddress ?? FARMS_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    authority: { value: input.authority ?? null, isWritable: false },
    payer: { value: input.payer ?? null, isWritable: true },
    owner: { value: input.owner ?? null, isWritable: false },
    delegatee: { value: input.delegatee ?? null, isWritable: false },
    userState: { value: input.userState ?? null, isWritable: true },
    farmState: { value: input.farmState ?? null, isWritable: true },
    systemProgram: { value: input.systemProgram ?? null, isWritable: false },
    rent: { value: input.rent ?? 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">;
  }
  if (!accounts.rent.value) {
    accounts.rent.value =
      "SysvarRent111111111111111111111111111111111" as Address<"SysvarRent111111111111111111111111111111111">;
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.authority),
      getAccountMeta(accounts.payer),
      getAccountMeta(accounts.owner),
      getAccountMeta(accounts.delegatee),
      getAccountMeta(accounts.userState),
      getAccountMeta(accounts.farmState),
      getAccountMeta(accounts.systemProgram),
      getAccountMeta(accounts.rent),
    ],
    data: getInitializeUserInstructionDataEncoder().encode({}),
    programAddress,
  } as InitializeUserInstruction<
    TProgramAddress,
    TAccountAuthority,
    TAccountPayer,
    TAccountOwner,
    TAccountDelegatee,
    TAccountUserState,
    TAccountFarmState,
    TAccountSystemProgram,
    TAccountRent
  >);
}

export type ParsedInitializeUserInstruction<
  TProgram extends string = typeof FARMS_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    authority: TAccountMetas[0];
    payer: TAccountMetas[1];
    owner: TAccountMetas[2];
    delegatee: TAccountMetas[3];
    userState: TAccountMetas[4];
    farmState: TAccountMetas[5];
    systemProgram: TAccountMetas[6];
    rent: TAccountMetas[7];
  };
  data: InitializeUserInstructionData;
};

export function parseInitializeUserInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedInitializeUserInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 8) {
    // 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: {
      authority: getNextAccount(),
      payer: getNextAccount(),
      owner: getNextAccount(),
      delegatee: getNextAccount(),
      userState: getNextAccount(),
      farmState: getNextAccount(),
      systemProgram: getNextAccount(),
      rent: getNextAccount(),
    },
    data: getInitializeUserInstructionDataDecoder().decode(instruction.data),
  };
}
