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

export const REWARD_USER_ONCE_DISCRIMINATOR = new Uint8Array([
  219, 137, 57, 22, 94, 186, 96, 114,
]);

export function getRewardUserOnceDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    REWARD_USER_ONCE_DISCRIMINATOR,
  );
}

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

export type RewardUserOnceInstructionData = {
  discriminator: ReadonlyUint8Array;
  rewardIndex: bigint;
  amount: bigint;
  expectedRewardsIssuedCumulative: bigint;
  userStateId: bigint;
};

export type RewardUserOnceInstructionDataArgs = {
  rewardIndex: number | bigint;
  amount: number | bigint;
  expectedRewardsIssuedCumulative: number | bigint;
  userStateId: number | bigint;
};

export function getRewardUserOnceInstructionDataEncoder(): FixedSizeEncoder<RewardUserOnceInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["rewardIndex", getU64Encoder()],
      ["amount", getU64Encoder()],
      ["expectedRewardsIssuedCumulative", getU64Encoder()],
      ["userStateId", getU64Encoder()],
    ]),
    (value) => ({ ...value, discriminator: REWARD_USER_ONCE_DISCRIMINATOR }),
  );
}

export function getRewardUserOnceInstructionDataDecoder(): FixedSizeDecoder<RewardUserOnceInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["rewardIndex", getU64Decoder()],
    ["amount", getU64Decoder()],
    ["expectedRewardsIssuedCumulative", getU64Decoder()],
    ["userStateId", getU64Decoder()],
  ]);
}

export function getRewardUserOnceInstructionDataCodec(): FixedSizeCodec<
  RewardUserOnceInstructionDataArgs,
  RewardUserOnceInstructionData
> {
  return combineCodec(
    getRewardUserOnceInstructionDataEncoder(),
    getRewardUserOnceInstructionDataDecoder(),
  );
}

export type RewardUserOnceInput<
  TAccountDelegateAuthority extends string = string,
  TAccountFarmState extends string = string,
  TAccountUserState extends string = string,
> = {
  delegateAuthority: TransactionSigner<TAccountDelegateAuthority>;
  farmState: Address<TAccountFarmState>;
  userState: Address<TAccountUserState>;
  rewardIndex: RewardUserOnceInstructionDataArgs["rewardIndex"];
  amount: RewardUserOnceInstructionDataArgs["amount"];
  expectedRewardsIssuedCumulative: RewardUserOnceInstructionDataArgs["expectedRewardsIssuedCumulative"];
  userStateId: RewardUserOnceInstructionDataArgs["userStateId"];
};

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

  // Original accounts.
  const originalAccounts = {
    delegateAuthority: {
      value: input.delegateAuthority ?? null,
      isWritable: true,
    },
    farmState: { value: input.farmState ?? null, isWritable: true },
    userState: { value: input.userState ?? 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.farmState),
      getAccountMeta(accounts.userState),
    ],
    data: getRewardUserOnceInstructionDataEncoder().encode(
      args as RewardUserOnceInstructionDataArgs,
    ),
    programAddress,
  } as RewardUserOnceInstruction<
    TProgramAddress,
    TAccountDelegateAuthority,
    TAccountFarmState,
    TAccountUserState
  >);
}

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

export function parseRewardUserOnceInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedRewardUserOnceInstruction<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(),
      farmState: getNextAccount(),
      userState: getNextAccount(),
    },
    data: getRewardUserOnceInstructionDataDecoder().decode(instruction.data),
  };
}
