export interface CppRegexModule {
  readonly regexToolsCppIdentity: () => Readonly<Record<string, unknown>>;
  readonly regexToolsCppRun: (request: unknown) => unknown;
}

export type CppRegexModuleFactory = (options: {
  readonly locateFile: (name: string) => string;
  readonly print: (line: string) => void;
  readonly printErr: (line: string) => void;
}) => Promise<CppRegexModule>;

export async function importCppRegexModuleFactory(
  moduleUrl: URL,
): Promise<CppRegexModuleFactory> {
  const imported = (await import(
    /* @vite-ignore */ moduleUrl.href
  )) as Partial<{ default: CppRegexModuleFactory }>;
  if (typeof imported.default !== "function") {
    throw new Error("The bundled C++ module has no Emscripten factory.");
  }
  return imported.default;
}
