/**
 * 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 {
  addDecoderSizePrefix,
  addEncoderSizePrefix,
  combineCodec,
  fixDecoderSize,
  fixEncoderSize,
  getBytesDecoder,
  getBytesEncoder,
  getStructDecoder,
  getStructEncoder,
  getU16Decoder,
  getU16Encoder,
  getU32Decoder,
  getU32Encoder,
  transformEncoder,
  type AccountMeta,
  type AccountSignerMeta,
  type Address,
  type Codec,
  type Decoder,
  type Encoder,
  type Instruction,
  type InstructionWithAccounts,
  type InstructionWithData,
  type ReadonlyAccount,
  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 UPDATE_FARM_CONFIG_DISCRIMINATOR = new Uint8Array([
  214, 176, 188, 244, 203, 59, 230, 207,
]);

export function getUpdateFarmConfigDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    UPDATE_FARM_CONFIG_DISCRIMINATOR,
  );
}

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

export type UpdateFarmConfigInstructionData = {
  discriminator: ReadonlyUint8Array;
  mode: number;
  data: ReadonlyUint8Array;
};

export type UpdateFarmConfigInstructionDataArgs = {
  mode: number;
  data: ReadonlyUint8Array;
};

export function getUpdateFarmConfigInstructionDataEncoder(): Encoder<UpdateFarmConfigInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["mode", getU16Encoder()],
      ["data", addEncoderSizePrefix(getBytesEncoder(), getU32Encoder())],
    ]),
    (value) => ({ ...value, discriminator: UPDATE_FARM_CONFIG_DISCRIMINATOR }),
  );
}

export function getUpdateFarmConfigInstructionDataDecoder(): Decoder<UpdateFarmConfigInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["mode", getU16Decoder()],
    ["data", addDecoderSizePrefix(getBytesDecoder(), getU32Decoder())],
  ]);
}

export function getUpdateFarmConfigInstructionDataCodec(): Codec<
  UpdateFarmConfigInstructionDataArgs,
  UpdateFarmConfigInstructionData
> {
  return combineCodec(
    getUpdateFarmConfigInstructionDataEncoder(),
    getUpdateFarmConfigInstructionDataDecoder(),
  );
}

export type UpdateFarmConfigInput<
  TAccountSigner extends string = string,
  TAccountFarmState extends string = string,
  TAccountScopePrices extends string = string,
> = {
  signer: TransactionSigner<TAccountSigner>;
  farmState: Address<TAccountFarmState>;
  scopePrices?: Address<TAccountScopePrices>;
  mode: UpdateFarmConfigInstructionDataArgs["mode"];
  data: UpdateFarmConfigInstructionDataArgs["data"];
};

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

  // Original accounts.
  const originalAccounts = {
    signer: { value: input.signer ?? null, isWritable: true },
    farmState: { value: input.farmState ?? null, isWritable: true },
    scopePrices: { value: input.scopePrices ?? null, isWritable: false },
  };
  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.signer),
      getAccountMeta(accounts.farmState),
      getAccountMeta(accounts.scopePrices),
    ],
    data: getUpdateFarmConfigInstructionDataEncoder().encode(
      args as UpdateFarmConfigInstructionDataArgs,
    ),
    programAddress,
  } as UpdateFarmConfigInstruction<
    TProgramAddress,
    TAccountSigner,
    TAccountFarmState,
    TAccountScopePrices
  >);
}

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

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