/**
 * 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 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_SECOND_DELEGATED_AUTHORITY_DISCRIMINATOR = new Uint8Array([
  127, 26, 6, 181, 203, 248, 117, 64,
]);

export function getUpdateSecondDelegatedAuthorityDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    UPDATE_SECOND_DELEGATED_AUTHORITY_DISCRIMINATOR,
  );
}

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

export type UpdateSecondDelegatedAuthorityInstructionData = {
  discriminator: ReadonlyUint8Array;
};

export type UpdateSecondDelegatedAuthorityInstructionDataArgs = {};

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

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

export function getUpdateSecondDelegatedAuthorityInstructionDataCodec(): FixedSizeCodec<
  UpdateSecondDelegatedAuthorityInstructionDataArgs,
  UpdateSecondDelegatedAuthorityInstructionData
> {
  return combineCodec(
    getUpdateSecondDelegatedAuthorityInstructionDataEncoder(),
    getUpdateSecondDelegatedAuthorityInstructionDataDecoder(),
  );
}

export type UpdateSecondDelegatedAuthorityInput<
  TAccountGlobalAdmin extends string = string,
  TAccountFarmState extends string = string,
  TAccountGlobalConfig extends string = string,
  TAccountNewSecondDelegatedAuthority extends string = string,
> = {
  globalAdmin: TransactionSigner<TAccountGlobalAdmin>;
  farmState: Address<TAccountFarmState>;
  globalConfig: Address<TAccountGlobalConfig>;
  newSecondDelegatedAuthority: Address<TAccountNewSecondDelegatedAuthority>;
};

export function getUpdateSecondDelegatedAuthorityInstruction<
  TAccountGlobalAdmin extends string,
  TAccountFarmState extends string,
  TAccountGlobalConfig extends string,
  TAccountNewSecondDelegatedAuthority extends string,
  TProgramAddress extends Address = typeof FARMS_PROGRAM_ADDRESS,
>(
  input: UpdateSecondDelegatedAuthorityInput<
    TAccountGlobalAdmin,
    TAccountFarmState,
    TAccountGlobalConfig,
    TAccountNewSecondDelegatedAuthority
  >,
  config?: { programAddress?: TProgramAddress },
): UpdateSecondDelegatedAuthorityInstruction<
  TProgramAddress,
  TAccountGlobalAdmin,
  TAccountFarmState,
  TAccountGlobalConfig,
  TAccountNewSecondDelegatedAuthority
> {
  // Program address.
  const programAddress = config?.programAddress ?? FARMS_PROGRAM_ADDRESS;

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

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.globalAdmin),
      getAccountMeta(accounts.farmState),
      getAccountMeta(accounts.globalConfig),
      getAccountMeta(accounts.newSecondDelegatedAuthority),
    ],
    data: getUpdateSecondDelegatedAuthorityInstructionDataEncoder().encode({}),
    programAddress,
  } as UpdateSecondDelegatedAuthorityInstruction<
    TProgramAddress,
    TAccountGlobalAdmin,
    TAccountFarmState,
    TAccountGlobalConfig,
    TAccountNewSecondDelegatedAuthority
  >);
}

export type ParsedUpdateSecondDelegatedAuthorityInstruction<
  TProgram extends string = typeof FARMS_PROGRAM_ADDRESS,
  TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
> = {
  programAddress: Address<TProgram>;
  accounts: {
    globalAdmin: TAccountMetas[0];
    farmState: TAccountMetas[1];
    globalConfig: TAccountMetas[2];
    newSecondDelegatedAuthority: TAccountMetas[3];
  };
  data: UpdateSecondDelegatedAuthorityInstructionData;
};

export function parseUpdateSecondDelegatedAuthorityInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedUpdateSecondDelegatedAuthorityInstruction<TProgram, TAccountMetas> {
  if (instruction.accounts.length < 4) {
    // 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(),
      farmState: getNextAccount(),
      globalConfig: getNextAccount(),
      newSecondDelegatedAuthority: getNextAccount(),
    },
    data: getUpdateSecondDelegatedAuthorityInstructionDataDecoder().decode(
      instruction.data,
    ),
  };
}
