import { useEffect, useState } from "react";
import type { RuntimeInfo } from "../../../types/global";
import type { RuntimeRepositoryPort } from "../../../infrastructure/repositories/rendererBridgeRepositoryTypes";

export function useScanRuntimeInfo(runtimeRepo?: RuntimeRepositoryPort) {
  const [runtimeInfo, setRuntimeInfo] = useState<RuntimeInfo | null>(null);

  useEffect(() => {
    let mounted = true;
    runtimeRepo?.getRuntimeInfo().then((result) => {
      if (mounted) {
        setRuntimeInfo(result);
      }
    }).catch(() => undefined);

    const timer = window.setInterval(() => {
      runtimeRepo?.getRuntimeInfo().then((result) => {
        if (mounted) setRuntimeInfo(result);
      }).catch(() => undefined);
    }, 5000);

    return () => {
      mounted = false;
      window.clearInterval(timer);
    };
  }, [runtimeRepo]);

  return runtimeInfo;
}

