/**
 * 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 {
  containsBytes,
  fixEncoderSize,
  getBytesEncoder,
  type Address,
  type Instruction,
  type InstructionWithData,
  type ReadonlyUint8Array,
} from "@solana/kit";
import {
  parseHandleV3AcrossMessageInstruction,
  type ParsedHandleV3AcrossMessageInstruction,
} from "../instructions";

export const MULTICALL_HANDLER_PROGRAM_ADDRESS =
  "HaQe51FWtnmaEcuYEfPA7MRCXKrtqptat4oJdJ8zV5Be" as Address<"HaQe51FWtnmaEcuYEfPA7MRCXKrtqptat4oJdJ8zV5Be">;

export enum MulticallHandlerInstruction {
  HandleV3AcrossMessage,
}

export function identifyMulticallHandlerInstruction(
  instruction: { data: ReadonlyUint8Array } | ReadonlyUint8Array,
): MulticallHandlerInstruction {
  const data = "data" in instruction ? instruction.data : instruction;
  if (
    containsBytes(
      data,
      fixEncoderSize(getBytesEncoder(), 8).encode(
        new Uint8Array([131, 141, 52, 71, 16, 59, 196, 92]),
      ),
      0,
    )
  ) {
    return MulticallHandlerInstruction.HandleV3AcrossMessage;
  }
  throw new Error(
    "The provided instruction could not be identified as a multicallHandler instruction.",
  );
}

export type ParsedMulticallHandlerInstruction<
  TProgram extends string = "HaQe51FWtnmaEcuYEfPA7MRCXKrtqptat4oJdJ8zV5Be",
> = {
  instructionType: MulticallHandlerInstruction.HandleV3AcrossMessage;
} & ParsedHandleV3AcrossMessageInstruction<TProgram>;

export function parseMulticallHandlerInstruction<TProgram extends string>(
  instruction: Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array>,
): ParsedMulticallHandlerInstruction<TProgram> {
  const instructionType = identifyMulticallHandlerInstruction(instruction);
  switch (instructionType) {
    case MulticallHandlerInstruction.HandleV3AcrossMessage: {
      return {
        instructionType: MulticallHandlerInstruction.HandleV3AcrossMessage,
        ...parseHandleV3AcrossMessageInstruction(instruction),
      };
    }
    default:
      throw new Error(
        `Unrecognized instruction type: ${instructionType as string}`,
      );
  }
}
