/**
 * 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 Address,
  type FixedSizeCodec,
  type FixedSizeDecoder,
  type FixedSizeEncoder,
  type Instruction,
  type InstructionWithAccounts,
  type InstructionWithData,
  type ReadonlyAccount,
  type ReadonlyUint8Array,
  type WritableAccount,
} from "@solana/kit";
import { FARMS_PROGRAM_ADDRESS } from "../programs";
import { getAccountMetaFactory, type ResolvedAccount } from "../shared";

export const REFRESH_FARM_DISCRIMINATOR = new Uint8Array([
  214, 131, 138, 183, 144, 194, 172, 42,
]);

export function getRefreshFarmDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    REFRESH_FARM_DISCRIMINATOR,
  );
}

export type RefreshFarmInstruction<
  TProgram extends string = typeof FARMS_PROGRAM_ADDRESS,
  TAccountFarmState extends string | AccountMeta<string> = string,
  TAccountScopePrices extends string | AccountMeta<string> = string,
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountFarmState extends string
        ? WritableAccount<TAccountFarmState>
        : TAccountFarmState,
      TAccountScopePrices extends string
        ? ReadonlyAccount<TAccountScopePrices>
        : TAccountScopePrices,
      ...TRemainingAccounts,
    ]
  >;

export type RefreshFarmInstructionData = { discriminator: ReadonlyUint8Array };

export type RefreshFarmInstructionDataArgs = {};

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

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

export function getRefreshFarmInstructionDataCodec(): FixedSizeCodec<
  RefreshFarmInstructionDataArgs,
  RefreshFarmInstructionData
> {
  return combineCodec(
    getRefreshFarmInstructionDataEncoder(),
    getRefreshFarmInstructionDataDecoder(),
  );
}

export type RefreshFarmInput<
  TAccountFarmState extends string = string,
  TAccountScopePrices extends string = string,
> = {
  farmState: Address<TAccountFarmState>;
  scopePrices?: Address<TAccountScopePrices>;
};

export function getRefreshFarmInstruction<
  TAccountFarmState extends string,
  TAccountScopePrices extends string,
  TProgramAddress extends Address = typeof FARMS_PROGRAM_ADDRESS,
>(
  input: RefreshFarmInput<TAccountFarmState, TAccountScopePrices>,
  config?: { programAddress?: TProgramAddress },
): RefreshFarmInstruction<
  TProgramAddress,
  TAccountFarmState,
  TAccountScopePrices
> {
  // Program address.
  const programAddress = config?.programAddress ?? FARMS_PROGRAM_ADDRESS;

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

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.farmState),
      getAccountMeta(accounts.scopePrices),
    ],
    data: getRefreshFarmInstructionDataEncoder().encode({}),
    programAddress,
  } as RefreshFarmInstruction<
    TProgramAddress,
    TAccountFarmState,
    TAccountScopePrices
  >);
}

export type ParsedRefreshFarmInstruction<
  TProgram extends string = typeof FARMS_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    farmState: TAccountMetas[0];
    scopePrices?: TAccountMetas[1] | undefined;
  };
  data: RefreshFarmInstructionData;
};

export function parseRefreshFarmInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedRefreshFarmInstruction<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 as TAccountMetas)[accountIndex]!;
    accountIndex += 1;
    return accountMeta;
  };
  const getNextOptionalAccount = () => {
    const accountMeta = getNextAccount();
    return accountMeta.address === FARMS_PROGRAM_ADDRESS
      ? undefined
      : accountMeta;
  };
  return {
    programAddress: instruction.programAddress,
    accounts: {
      farmState: getNextAccount(),
      scopePrices: getNextOptionalAccount(),
    },
    data: getRefreshFarmInstructionDataDecoder().decode(instruction.data),
  };
}
