/**
 * 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 ReadonlyAccount,
  type ReadonlySignerAccount,
  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 TRANSFER_OWNERSHIP_DISCRIMINATOR = new Uint8Array([
  65, 177, 215, 73, 53, 45, 99, 47,
]);

export function getTransferOwnershipDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    TRANSFER_OWNERSHIP_DISCRIMINATOR,
  );
}

export type TransferOwnershipInstruction<
  TProgram extends string = typeof FARMS_PROGRAM_ADDRESS,
  TAccountOldOwner extends string | AccountMeta<string> = string,
  TAccountPayer extends string | AccountMeta<string> = string,
  TAccountNewOwner extends string | AccountMeta<string> = string,
  TAccountOldUserState extends string | AccountMeta<string> = string,
  TAccountNewUserState extends string | AccountMeta<string> = string,
  TAccountFarmState extends string | AccountMeta<string> = string,
  TAccountScopePrices extends string | AccountMeta<string> = string,
  TAccountSystemProgram extends string | AccountMeta<string> =
    "11111111111111111111111111111111",
  TAccountRent extends string | AccountMeta<string> =
    "SysvarRent111111111111111111111111111111111",
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<
    [
      TAccountOldOwner extends string
        ? ReadonlySignerAccount<TAccountOldOwner> &
            AccountSignerMeta<TAccountOldOwner>
        : TAccountOldOwner,
      TAccountPayer extends string
        ? WritableSignerAccount<TAccountPayer> &
            AccountSignerMeta<TAccountPayer>
        : TAccountPayer,
      TAccountNewOwner extends string
        ? ReadonlyAccount<TAccountNewOwner>
        : TAccountNewOwner,
      TAccountOldUserState extends string
        ? WritableAccount<TAccountOldUserState>
        : TAccountOldUserState,
      TAccountNewUserState extends string
        ? WritableAccount<TAccountNewUserState>
        : TAccountNewUserState,
      TAccountFarmState extends string
        ? WritableAccount<TAccountFarmState>
        : TAccountFarmState,
      TAccountScopePrices extends string
        ? ReadonlyAccount<TAccountScopePrices>
        : TAccountScopePrices,
      TAccountSystemProgram extends string
        ? ReadonlyAccount<TAccountSystemProgram>
        : TAccountSystemProgram,
      TAccountRent extends string
        ? ReadonlyAccount<TAccountRent>
        : TAccountRent,
      ...TRemainingAccounts,
    ]
  >;

export type TransferOwnershipInstructionData = {
  discriminator: ReadonlyUint8Array;
};

export type TransferOwnershipInstructionDataArgs = {};

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

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

export function getTransferOwnershipInstructionDataCodec(): FixedSizeCodec<
  TransferOwnershipInstructionDataArgs,
  TransferOwnershipInstructionData
> {
  return combineCodec(
    getTransferOwnershipInstructionDataEncoder(),
    getTransferOwnershipInstructionDataDecoder(),
  );
}

export type TransferOwnershipInput<
  TAccountOldOwner extends string = string,
  TAccountPayer extends string = string,
  TAccountNewOwner extends string = string,
  TAccountOldUserState extends string = string,
  TAccountNewUserState extends string = string,
  TAccountFarmState extends string = string,
  TAccountScopePrices extends string = string,
  TAccountSystemProgram extends string = string,
  TAccountRent extends string = string,
> = {
  oldOwner: TransactionSigner<TAccountOldOwner>;
  payer: TransactionSigner<TAccountPayer>;
  newOwner: Address<TAccountNewOwner>;
  oldUserState: Address<TAccountOldUserState>;
  newUserState: Address<TAccountNewUserState>;
  farmState: Address<TAccountFarmState>;
  scopePrices?: Address<TAccountScopePrices>;
  systemProgram?: Address<TAccountSystemProgram>;
  rent?: Address<TAccountRent>;
};

export function getTransferOwnershipInstruction<
  TAccountOldOwner extends string,
  TAccountPayer extends string,
  TAccountNewOwner extends string,
  TAccountOldUserState extends string,
  TAccountNewUserState extends string,
  TAccountFarmState extends string,
  TAccountScopePrices extends string,
  TAccountSystemProgram extends string,
  TAccountRent extends string,
  TProgramAddress extends Address = typeof FARMS_PROGRAM_ADDRESS,
>(
  input: TransferOwnershipInput<
    TAccountOldOwner,
    TAccountPayer,
    TAccountNewOwner,
    TAccountOldUserState,
    TAccountNewUserState,
    TAccountFarmState,
    TAccountScopePrices,
    TAccountSystemProgram,
    TAccountRent
  >,
  config?: { programAddress?: TProgramAddress },
): TransferOwnershipInstruction<
  TProgramAddress,
  TAccountOldOwner,
  TAccountPayer,
  TAccountNewOwner,
  TAccountOldUserState,
  TAccountNewUserState,
  TAccountFarmState,
  TAccountScopePrices,
  TAccountSystemProgram,
  TAccountRent
> {
  // Program address.
  const programAddress = config?.programAddress ?? FARMS_PROGRAM_ADDRESS;

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

  // Resolve default values.
  if (!accounts.systemProgram.value) {
    accounts.systemProgram.value =
      "11111111111111111111111111111111" as Address<"11111111111111111111111111111111">;
  }
  if (!accounts.rent.value) {
    accounts.rent.value =
      "SysvarRent111111111111111111111111111111111" as Address<"SysvarRent111111111111111111111111111111111">;
  }

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.oldOwner),
      getAccountMeta(accounts.payer),
      getAccountMeta(accounts.newOwner),
      getAccountMeta(accounts.oldUserState),
      getAccountMeta(accounts.newUserState),
      getAccountMeta(accounts.farmState),
      getAccountMeta(accounts.scopePrices),
      getAccountMeta(accounts.systemProgram),
      getAccountMeta(accounts.rent),
    ],
    data: getTransferOwnershipInstructionDataEncoder().encode({}),
    programAddress,
  } as TransferOwnershipInstruction<
    TProgramAddress,
    TAccountOldOwner,
    TAccountPayer,
    TAccountNewOwner,
    TAccountOldUserState,
    TAccountNewUserState,
    TAccountFarmState,
    TAccountScopePrices,
    TAccountSystemProgram,
    TAccountRent
  >);
}

export type ParsedTransferOwnershipInstruction<
  TProgram extends string = typeof FARMS_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    oldOwner: TAccountMetas[0];
    payer: TAccountMetas[1];
    newOwner: TAccountMetas[2];
    oldUserState: TAccountMetas[3];
    newUserState: TAccountMetas[4];
    farmState: TAccountMetas[5];
    scopePrices?: TAccountMetas[6] | undefined;
    systemProgram: TAccountMetas[7];
    rent: TAccountMetas[8];
  };
  data: TransferOwnershipInstructionData;
};

export function parseTransferOwnershipInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedTransferOwnershipInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 9) {
    // 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: {
      oldOwner: getNextAccount(),
      payer: getNextAccount(),
      newOwner: getNextAccount(),
      oldUserState: getNextAccount(),
      newUserState: getNextAccount(),
      farmState: getNextAccount(),
      scopePrices: getNextOptionalAccount(),
      systemProgram: getNextAccount(),
      rent: getNextAccount(),
    },
    data: getTransferOwnershipInstructionDataDecoder().decode(instruction.data),
  };
}
