/**
 * 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";

export const UPDATE_GLOBAL_CONFIG_ADMIN_DISCRIMINATOR = new Uint8Array([
  184, 87, 23, 193, 156, 238, 175, 119,
]);

export function getUpdateGlobalConfigAdminDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    UPDATE_GLOBAL_CONFIG_ADMIN_DISCRIMINATOR,
  );
}

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

export type UpdateGlobalConfigAdminInstructionData = {
  discriminator: ReadonlyUint8Array;
};

export type UpdateGlobalConfigAdminInstructionDataArgs = {};

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

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

export function getUpdateGlobalConfigAdminInstructionDataCodec(): FixedSizeCodec<
  UpdateGlobalConfigAdminInstructionDataArgs,
  UpdateGlobalConfigAdminInstructionData
> {
  return combineCodec(
    getUpdateGlobalConfigAdminInstructionDataEncoder(),
    getUpdateGlobalConfigAdminInstructionDataDecoder(),
  );
}

export type UpdateGlobalConfigAdminInput<
  TAccountPendingGlobalAdmin extends string = string,
  TAccountGlobalConfig extends string = string,
> = {
  pendingGlobalAdmin: TransactionSigner<TAccountPendingGlobalAdmin>;
  globalConfig: Address<TAccountGlobalConfig>;
};

export function getUpdateGlobalConfigAdminInstruction<
  TAccountPendingGlobalAdmin extends string,
  TAccountGlobalConfig extends string,
  TProgramAddress extends Address = typeof FARMS_PROGRAM_ADDRESS,
>(
  input: UpdateGlobalConfigAdminInput<
    TAccountPendingGlobalAdmin,
    TAccountGlobalConfig
  >,
  config?: { programAddress?: TProgramAddress },
): UpdateGlobalConfigAdminInstruction<
  TProgramAddress,
  TAccountPendingGlobalAdmin,
  TAccountGlobalConfig
> {
  // Program address.
  const programAddress = config?.programAddress ?? FARMS_PROGRAM_ADDRESS;

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

  const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
  return Object.freeze({
    accounts: [
      getAccountMeta(accounts.pendingGlobalAdmin),
      getAccountMeta(accounts.globalConfig),
    ],
    data: getUpdateGlobalConfigAdminInstructionDataEncoder().encode({}),
    programAddress,
  } as UpdateGlobalConfigAdminInstruction<
    TProgramAddress,
    TAccountPendingGlobalAdmin,
    TAccountGlobalConfig
  >);
}

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

export function parseUpdateGlobalConfigAdminInstruction<
  TProgram extends string,
  TAccountMetas extends readonly AccountMeta[],
>(
  instruction: Instruction<TProgram> &
    InstructionWithAccounts<TAccountMetas> &
    InstructionWithData<ReadonlyUint8Array>,
): ParsedUpdateGlobalConfigAdminInstruction<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: {
      pendingGlobalAdmin: getNextAccount(),
      globalConfig: getNextAccount(),
    },
    data: getUpdateGlobalConfigAdminInstructionDataDecoder().decode(
      instruction.data,
    ),
  };
}
