import { store } from "@/redux/store";
import { updateHeap } from "@/redux/slices/heapMonitorSlice";

export const monitorHeapAndReload = (limitInGB = 8, intervalMs = 10000) => {
  const limit = limitInGB * 1024 * 1024 * 1024;

  return setInterval(() => {
    const heap = performance.memory?.usedJSHeapSize || 0;

    //console.log("Heap:", (heap / 1024 / 1024).toFixed(2), "MB");

    if (heap > limit) {
      console.warn("🚨 Heap-Limit erreicht. Reload wird ausgeführt.");
      location.reload();
    }
    store.dispatch(updateHeap(heap));
  }, intervalMs);
};

export const monitorHeapWithRedux = (intervalMs = 10000) => {
  return setInterval(() => {
    const heap = performance.memory?.usedJSHeapSize || 0;
    store.dispatch(updateHeap(heap));
  }, intervalMs);
};
