/**
 * 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 {
  addDecoderSizePrefix,
  addEncoderSizePrefix,
  combineCodec,
  fixDecoderSize,
  fixEncoderSize,
  getBytesDecoder,
  getBytesEncoder,
  getStructDecoder,
  getStructEncoder,
  getU32Decoder,
  getU32Encoder,
  transformEncoder,
  type AccountMeta,
  type Address,
  type Codec,
  type Decoder,
  type Encoder,
  type Instruction,
  type InstructionWithAccounts,
  type InstructionWithData,
  type ReadonlyUint8Array,
} from "@solana/kit";
import { MULTICALL_HANDLER_PROGRAM_ADDRESS } from "../programs";

export const HANDLE_V3_ACROSS_MESSAGE_DISCRIMINATOR = new Uint8Array([
  131, 141, 52, 71, 16, 59, 196, 92,
]);

export function getHandleV3AcrossMessageDiscriminatorBytes() {
  return fixEncoderSize(getBytesEncoder(), 8).encode(
    HANDLE_V3_ACROSS_MESSAGE_DISCRIMINATOR,
  );
}

export type HandleV3AcrossMessageInstruction<
  TProgram extends string = typeof MULTICALL_HANDLER_PROGRAM_ADDRESS,
  TRemainingAccounts extends readonly AccountMeta<string>[] = [],
> = Instruction<TProgram> &
  InstructionWithData<ReadonlyUint8Array> &
  InstructionWithAccounts<TRemainingAccounts>;

export type HandleV3AcrossMessageInstructionData = {
  discriminator: ReadonlyUint8Array;
  message: ReadonlyUint8Array;
};

export type HandleV3AcrossMessageInstructionDataArgs = {
  message: ReadonlyUint8Array;
};

export function getHandleV3AcrossMessageInstructionDataEncoder(): Encoder<HandleV3AcrossMessageInstructionDataArgs> {
  return transformEncoder(
    getStructEncoder([
      ["discriminator", fixEncoderSize(getBytesEncoder(), 8)],
      ["message", addEncoderSizePrefix(getBytesEncoder(), getU32Encoder())],
    ]),
    (value) => ({
      ...value,
      discriminator: HANDLE_V3_ACROSS_MESSAGE_DISCRIMINATOR,
    }),
  );
}

export function getHandleV3AcrossMessageInstructionDataDecoder(): Decoder<HandleV3AcrossMessageInstructionData> {
  return getStructDecoder([
    ["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
    ["message", addDecoderSizePrefix(getBytesDecoder(), getU32Decoder())],
  ]);
}

export function getHandleV3AcrossMessageInstructionDataCodec(): Codec<
  HandleV3AcrossMessageInstructionDataArgs,
  HandleV3AcrossMessageInstructionData
> {
  return combineCodec(
    getHandleV3AcrossMessageInstructionDataEncoder(),
    getHandleV3AcrossMessageInstructionDataDecoder(),
  );
}

export type HandleV3AcrossMessageInput = {
  message: HandleV3AcrossMessageInstructionDataArgs["message"];
};

export function getHandleV3AcrossMessageInstruction<
  TProgramAddress extends Address = typeof MULTICALL_HANDLER_PROGRAM_ADDRESS,
>(
  input: HandleV3AcrossMessageInput,
  config?: { programAddress?: TProgramAddress },
): HandleV3AcrossMessageInstruction<TProgramAddress> {
  // Program address.
  const programAddress =
    config?.programAddress ?? MULTICALL_HANDLER_PROGRAM_ADDRESS;

  // Original args.
  const args = { ...input };

  return Object.freeze({
    data: getHandleV3AcrossMessageInstructionDataEncoder().encode(
      args as HandleV3AcrossMessageInstructionDataArgs,
    ),
    programAddress,
  } as HandleV3AcrossMessageInstruction<TProgramAddress>);
}

export type ParsedHandleV3AcrossMessageInstruction<
  TProgram extends string = typeof MULTICALL_HANDLER_PROGRAM_ADDRESS,
> = {
  programAddress: Address<TProgram>;
  data: HandleV3AcrossMessageInstructionData;
};

export function parseHandleV3AcrossMessageInstruction<TProgram extends string>(
  instruction: Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array>,
): ParsedHandleV3AcrossMessageInstruction<TProgram> {
  return {
    programAddress: instruction.programAddress,
    data: getHandleV3AcrossMessageInstructionDataDecoder().decode(
      instruction.data,
    ),
  };
}
