/**
 * 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 ReadonlySignerAccount,
  type ReadonlyUint8Array,
  type TransactionSigner,
  type WritableAccount,
} from "@solana/kit";
import { FARMS_PROGRAM_ADDRESS } from "../programs";
import { getAccountMetaFactory, type ResolvedAccount } from "../shared";
import {
  getFarmConfigOptionDecoder,
  getFarmConfigOptionEncoder,
  getGlobalConfigOptionDecoder,
  getGlobalConfigOptionEncoder,
  getLockingModeDecoder,
  getLockingModeEncoder,
  getRewardTypeDecoder,
  getRewardTypeEncoder,
  getTimeUnitDecoder,
  getTimeUnitEncoder,
  type FarmConfigOption,
  type FarmConfigOptionArgs,
  type GlobalConfigOption,
  type GlobalConfigOptionArgs,
  type LockingMode,
  type LockingModeArgs,
  type RewardType,
  type RewardTypeArgs,
  type TimeUnit,
  type TimeUnitArgs,
} from "../types";

export const IDL_MISSING_TYPES_DISCRIMINATOR = new Uint8Array([
  130, 80, 38, 153, 80, 212, 182, 253,
]);

export function getIdlMissingTypesDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    IDL_MISSING_TYPES_DISCRIMINATOR,
  );
}

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

export type IdlMissingTypesInstructionData = {
  discriminator: ReadonlyUint8Array;
  globalConfigOptionKind: GlobalConfigOption;
  farmConfigOptionKind: FarmConfigOption;
  timeUnit: TimeUnit;
  lockingMode: LockingMode;
  rewardType: RewardType;
};

export type IdlMissingTypesInstructionDataArgs = {
  globalConfigOptionKind: GlobalConfigOptionArgs;
  farmConfigOptionKind: FarmConfigOptionArgs;
  timeUnit: TimeUnitArgs;
  lockingMode: LockingModeArgs;
  rewardType: RewardTypeArgs;
};

export function getIdlMissingTypesInstructionDataEncoder(): FixedSizeEncoder<IdlMissingTypesInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["globalConfigOptionKind", getGlobalConfigOptionEncoder()],
      ["farmConfigOptionKind", getFarmConfigOptionEncoder()],
      ["timeUnit", getTimeUnitEncoder()],
      ["lockingMode", getLockingModeEncoder()],
      ["rewardType", getRewardTypeEncoder()],
    ]),
    (value) => ({ ...value, discriminator: IDL_MISSING_TYPES_DISCRIMINATOR }),
  );
}

export function getIdlMissingTypesInstructionDataDecoder(): FixedSizeDecoder<IdlMissingTypesInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["globalConfigOptionKind", getGlobalConfigOptionDecoder()],
    ["farmConfigOptionKind", getFarmConfigOptionDecoder()],
    ["timeUnit", getTimeUnitDecoder()],
    ["lockingMode", getLockingModeDecoder()],
    ["rewardType", getRewardTypeDecoder()],
  ]);
}

export function getIdlMissingTypesInstructionDataCodec(): FixedSizeCodec<
  IdlMissingTypesInstructionDataArgs,
  IdlMissingTypesInstructionData
> {
  return combineCodec(
    getIdlMissingTypesInstructionDataEncoder(),
    getIdlMissingTypesInstructionDataDecoder(),
  );
}

export type IdlMissingTypesInput<
  TAccountGlobalAdmin extends string = string,
  TAccountGlobalConfig extends string = string,
> = {
  globalAdmin: TransactionSigner<TAccountGlobalAdmin>;
  globalConfig: Address<TAccountGlobalConfig>;
  globalConfigOptionKind: IdlMissingTypesInstructionDataArgs["globalConfigOptionKind"];
  farmConfigOptionKind: IdlMissingTypesInstructionDataArgs["farmConfigOptionKind"];
  timeUnit: IdlMissingTypesInstructionDataArgs["timeUnit"];
  lockingMode: IdlMissingTypesInstructionDataArgs["lockingMode"];
  rewardType: IdlMissingTypesInstructionDataArgs["rewardType"];
};

export function getIdlMissingTypesInstruction<
  TAccountGlobalAdmin extends string,
  TAccountGlobalConfig extends string,
  TProgramAddress extends Address = typeof FARMS_PROGRAM_ADDRESS,
>(
  input: IdlMissingTypesInput<TAccountGlobalAdmin, TAccountGlobalConfig>,
  config?: { programAddress?: TProgramAddress },
): IdlMissingTypesInstruction<
  TProgramAddress,
  TAccountGlobalAdmin,
  TAccountGlobalConfig
> {
  // Program address.
  const programAddress = config?.programAddress ?? FARMS_PROGRAM_ADDRESS;

  // Original accounts.
  const originalAccounts = {
    globalAdmin: { value: input.globalAdmin ?? null, isWritable: false },
    globalConfig: { value: input.globalConfig ?? 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.globalAdmin),
      getAccountMeta(accounts.globalConfig),
    ],
    data: getIdlMissingTypesInstructionDataEncoder().encode(
      args as IdlMissingTypesInstructionDataArgs,
    ),
    programAddress,
  } as IdlMissingTypesInstruction<
    TProgramAddress,
    TAccountGlobalAdmin,
    TAccountGlobalConfig
  >);
}

export type ParsedIdlMissingTypesInstruction<
  TProgram extends string = typeof FARMS_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    globalAdmin: TAccountMetas[0];
    globalConfig: TAccountMetas[1];
  };
  data: IdlMissingTypesInstructionData;
};

export function parseIdlMissingTypesInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedIdlMissingTypesInstruction<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;
  };
  return {
    programAddress: instruction.programAddress,
    accounts: { globalAdmin: getNextAccount(), globalConfig: getNextAccount() },
    data: getIdlMissingTypesInstructionDataDecoder().decode(instruction.data),
  };
}
