/**
 * 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,
  getStructDecoder,
  getStructEncoder,
  getU64Decoder,
  getU64Encoder,
  type Address,
  type Codec,
  type Decoder,
  type Encoder,
  type IAccountMeta,
  type IInstruction,
  type IInstructionWithAccounts,
  type IInstructionWithData,
} from '@solana/web3.js';
import { DUMMY_PROGRAM_ADDRESS } from '../programs';

export type Instruction4Instruction<
  TProgram extends string = typeof DUMMY_PROGRAM_ADDRESS,
  TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
> = IInstruction<TProgram> &
  IInstructionWithData<Uint8Array> &
  IInstructionWithAccounts<TRemainingAccounts>;

export type Instruction4InstructionData = { myArgument: bigint };

export type Instruction4InstructionDataArgs = { myArgument: number | bigint };

export function getInstruction4InstructionDataEncoder(): Encoder<Instruction4InstructionDataArgs> {
  return getStructEncoder([['myArgument', getU64Encoder()]]);
}

export function getInstruction4InstructionDataDecoder(): Decoder<Instruction4InstructionData> {
  return getStructDecoder([['myArgument', getU64Decoder()]]);
}

export function getInstruction4InstructionDataCodec(): Codec<
  Instruction4InstructionDataArgs,
  Instruction4InstructionData
> {
  return combineCodec(
    getInstruction4InstructionDataEncoder(),
    getInstruction4InstructionDataDecoder()
  );
}

export type Instruction4Input = {
  myArgument: Instruction4InstructionDataArgs['myArgument'];
};

export function getInstruction4Instruction<
  TProgramAddress extends Address = typeof DUMMY_PROGRAM_ADDRESS,
>(
  input: Instruction4Input,
  config?: { programAddress?: TProgramAddress }
): Instruction4Instruction<TProgramAddress> {
  // Program address.
  const programAddress = config?.programAddress ?? DUMMY_PROGRAM_ADDRESS;

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

  const instruction = {
    programAddress,
    data: getInstruction4InstructionDataEncoder().encode(
      args as Instruction4InstructionDataArgs
    ),
  } as Instruction4Instruction<TProgramAddress>;

  return instruction;
}

export type ParsedInstruction4Instruction<
  TProgram extends string = typeof DUMMY_PROGRAM_ADDRESS,
> = {
  programAddress: Address<TProgram>;
  data: Instruction4InstructionData;
};

export function parseInstruction4Instruction<TProgram extends string>(
  instruction: IInstruction<TProgram> & IInstructionWithData<Uint8Array>
): ParsedInstruction4Instruction<TProgram> {
  return {
    programAddress: instruction.programAddress,
    data: getInstruction4InstructionDataDecoder().decode(instruction.data),
  };
}
