/**
 * 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,
  getU64Decoder,
  getU64Encoder,
  transformEncoder,
  type AccountMeta,
  type AccountSignerMeta,
  type Address,
  type FixedSizeCodec,
  type FixedSizeDecoder,
  type FixedSizeEncoder,
  type Instruction,
  type InstructionWithAccounts,
  type InstructionWithData,
  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 SET_STAKE_DELEGATED_DISCRIMINATOR = new Uint8Array([
  73, 171, 184, 75, 30, 56, 198, 223,
]);

export function getSetStakeDelegatedDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    SET_STAKE_DELEGATED_DISCRIMINATOR,
  );
}

export type SetStakeDelegatedInstruction<
  TProgram extends string = typeof FARMS_PROGRAM_ADDRESS,
  TAccountDelegateAuthority extends string | AccountMeta<string> = string,
  TAccountUserState extends string | AccountMeta<string> = string,
  TAccountFarmState extends string | AccountMeta<string> = string,
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountDelegateAuthority extends string
        ? ReadonlySignerAccount<TAccountDelegateAuthority> &
            AccountSignerMeta<TAccountDelegateAuthority>
        : TAccountDelegateAuthority,
      TAccountUserState extends string
        ? WritableAccount<TAccountUserState>
        : TAccountUserState,
      TAccountFarmState extends string
        ? WritableAccount<TAccountFarmState>
        : TAccountFarmState,
      ...TRemainingAccounts,
    ]
  >;

export type SetStakeDelegatedInstructionData = {
  discriminator: ReadonlyUint8Array;
  newAmount: bigint;
};

export type SetStakeDelegatedInstructionDataArgs = {
  newAmount: number | bigint;
};

export function getSetStakeDelegatedInstructionDataEncoder(): FixedSizeEncoder<SetStakeDelegatedInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["newAmount", getU64Encoder()],
    ]),
    (value) => ({ ...value, discriminator: SET_STAKE_DELEGATED_DISCRIMINATOR }),
  );
}

export function getSetStakeDelegatedInstructionDataDecoder(): FixedSizeDecoder<SetStakeDelegatedInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["newAmount", getU64Decoder()],
  ]);
}

export function getSetStakeDelegatedInstructionDataCodec(): FixedSizeCodec<
  SetStakeDelegatedInstructionDataArgs,
  SetStakeDelegatedInstructionData
> {
  return combineCodec(
    getSetStakeDelegatedInstructionDataEncoder(),
    getSetStakeDelegatedInstructionDataDecoder(),
  );
}

export type SetStakeDelegatedInput<
  TAccountDelegateAuthority extends string = string,
  TAccountUserState extends string = string,
  TAccountFarmState extends string = string,
> = {
  delegateAuthority: TransactionSigner<TAccountDelegateAuthority>;
  userState: Address<TAccountUserState>;
  farmState: Address<TAccountFarmState>;
  newAmount: SetStakeDelegatedInstructionDataArgs["newAmount"];
};

export function getSetStakeDelegatedInstruction<
  TAccountDelegateAuthority extends string,
  TAccountUserState extends string,
  TAccountFarmState extends string,
  TProgramAddress extends Address = typeof FARMS_PROGRAM_ADDRESS,
>(
  input: SetStakeDelegatedInput<
    TAccountDelegateAuthority,
    TAccountUserState,
    TAccountFarmState
  >,
  config?: { programAddress?: TProgramAddress },
): SetStakeDelegatedInstruction<
  TProgramAddress,
  TAccountDelegateAuthority,
  TAccountUserState,
  TAccountFarmState
> {
  // Program address.
  const programAddress = config?.programAddress ?? FARMS_PROGRAM_ADDRESS;

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

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

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.delegateAuthority),
      getAccountMeta(accounts.userState),
      getAccountMeta(accounts.farmState),
    ],
    data: getSetStakeDelegatedInstructionDataEncoder().encode(
      args as SetStakeDelegatedInstructionDataArgs,
    ),
    programAddress,
  } as SetStakeDelegatedInstruction<
    TProgramAddress,
    TAccountDelegateAuthority,
    TAccountUserState,
    TAccountFarmState
  >);
}

export type ParsedSetStakeDelegatedInstruction<
  TProgram extends string = typeof FARMS_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    delegateAuthority: TAccountMetas[0];
    userState: TAccountMetas[1];
    farmState: TAccountMetas[2];
  };
  data: SetStakeDelegatedInstructionData;
};

export function parseSetStakeDelegatedInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedSetStakeDelegatedInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 3) {
    // 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: {
      delegateAuthority: getNextAccount(),
      userState: getNextAccount(),
      farmState: getNextAccount(),
    },
    data: getSetStakeDelegatedInstructionDataDecoder().decode(instruction.data),
  };
}
