export interface PythonCallableProxy {
  (
    operation: "identity" | "execute" | "replace",
    pattern: string,
    flags: string,
    subject: string,
    maximumMatches: number,
    maximumCaptureRows: number,
    replacement: string,
    maximumOutputBytes: number,
  ): unknown;
  destroy(): void;
}

export interface PyodideRuntime {
  readonly version: string;
  runPython(source: string): unknown;
}

export interface PyodideLoaderOptions {
  readonly indexURL: string;
  readonly stdout?: (line: string) => void;
  readonly stderr?: (line: string) => void;
}

export interface PyodideModule {
  readonly version: string;
  loadPyodide(options: PyodideLoaderOptions): Promise<PyodideRuntime>;
}

export async function importPyodideModule(
  moduleUrl: URL,
): Promise<PyodideModule> {
  const imported: unknown = await import(/* @vite-ignore */ moduleUrl.href);
  if (
    typeof imported !== "object" ||
    imported === null ||
    !("version" in imported) ||
    typeof imported.version !== "string" ||
    !("loadPyodide" in imported) ||
    typeof imported.loadPyodide !== "function"
  ) {
    throw new Error("The Python runtime module has an invalid shape.");
  }
  return imported as PyodideModule;
}
