// components/main/kabelueberwachung/kue705FO/hooks/useAlarmStatus.ts
import { useEffect, useState } from "react";

const useAlarmStatus = (
  slotIndex: number,
  kueAlarm1: number[],
  kueAlarm2: number[],
  kueCableBreak: number[],
  kueGroundFault: number[]
) => {
  const [currentAlarmStatus, setCurrentAlarmStatus] = useState(false);

  useEffect(() => {
    const calculateAlarmStatus = () => {
      return (
        (kueAlarm1?.[slotIndex] ?? 0) ||
        (kueAlarm2?.[slotIndex] ?? 0) ||
        (kueCableBreak?.[slotIndex] ?? 0) ||
        (kueGroundFault?.[slotIndex] ?? 0)
      );
    };

    const newStatus = !!calculateAlarmStatus();

    // 🚀 Nur aktualisieren, wenn sich der Wert wirklich ändert!
    setCurrentAlarmStatus((prevStatus) =>
      prevStatus !== newStatus ? newStatus : prevStatus
    );
  }, [slotIndex, kueAlarm1, kueAlarm2, kueCableBreak, kueGroundFault]);

  return currentAlarmStatus;
};

export default useAlarmStatus;
