import { useCallback, type MouseEvent } from "react";
import type { ScanSummaryModalProps } from "../types";

export interface ScanSummaryModalModel {
  closeSummary: () => void;
  stopPropagation: (event: MouseEvent<HTMLDivElement>) => void;
  iconKind: "running" | "blocked" | "stopped" | "finished";
  summaryTitle: string;
  gridSummaryText: string | null;
}

interface UseScanSummaryModalModelInput extends Pick<ScanSummaryModalProps, "setScanSummary"> {
  scanSummary: ScanSummaryModalProps["controller"]["scanSummary"];
}

export function useScanSummaryModalModel({
  setScanSummary,
  scanSummary,
}: UseScanSummaryModalModelInput): ScanSummaryModalModel {
  const closeSummary = useCallback(() => setScanSummary(null), [setScanSummary]);
  const stopPropagation = useCallback((event: MouseEvent<HTMLDivElement>) => {
    event.stopPropagation();
  }, []);
  const isBlocked = scanSummary?.status === "blocked";
  const isStopped = scanSummary?.status === "stopped";
  const summaryTitle = isStopped ? "Scan gestoppt" : isBlocked ? "Scan abgebrochen" : "Scan fertig";
  const iconKind = isBlocked ? "blocked" : isStopped ? "stopped" : "finished";
  const gridSummaryText = scanSummary?.gridLabel ?? null;

  return {
    closeSummary,
    stopPropagation,
    iconKind,
    summaryTitle,
    gridSummaryText,
  };
}
