export interface DotNetRegexExports {
  readonly RegexBridge: {
    readonly Identity: () => string;
    readonly Run: (requestJson: string) => string;
  };
}

export interface DotNetRuntime {
  readonly getAssemblyExports: (
    assemblyName: string,
  ) => Promise<DotNetRegexExports>;
  readonly getConfig: () => { readonly mainAssemblyName: string };
  readonly runMain: () => Promise<number>;
}

export interface DotNetBuilder {
  readonly withDiagnosticTracing: (enabled: boolean) => DotNetBuilder;
  readonly create: () => Promise<DotNetRuntime>;
}

export interface DotNetModule {
  readonly dotnet: DotNetBuilder;
}

export async function importDotNetModule(
  moduleUrl: URL,
): Promise<DotNetModule> {
  const imported = (await import(
    /* @vite-ignore */ moduleUrl.href
  )) as Partial<DotNetModule>;
  if (
    !imported.dotnet ||
    typeof imported.dotnet.create !== "function" ||
    typeof imported.dotnet.withDiagnosticTracing !== "function"
  ) {
    throw new Error("The bundled .NET module has no browser runtime builder.");
  }
  return imported as DotNetModule;
}
