import { useEffect } from "react";
import type { AutomationRepositoryPort } from "../../../infrastructure/repositories/rendererBridgeRepositoryTypes";

interface ScanCommandListenerInput {
  automationRepo?: AutomationRepositoryPort;
  autoScanRunning: boolean;
  isScanning: boolean;
  selectedSourceId: string;
  requestScanStop: (reason: string) => void;
  runVisibleGridScan: () => Promise<void>;
}

export function useScanCommandListener({
  automationRepo,
  autoScanRunning,
  isScanning,
  selectedSourceId,
  requestScanStop,
  runVisibleGridScan,
}: ScanCommandListenerInput) {
  useEffect(() => {
    if (!automationRepo?.onCommand) return;
    return automationRepo.onCommand((command: "start-auto" | "stop") => {
      if (command === "stop") {
        requestScanStop("Hotkey/Dev-Stop gedrueckt.");
        return;
      }
      if (command === "start-auto" && !autoScanRunning && !isScanning && selectedSourceId) {
        void runVisibleGridScan();
      }
    });
  }, [automationRepo, autoScanRunning, isScanning, selectedSourceId, requestScanStop, runVisibleGridScan]);
}

