// static/js/connection-check.js

let connectionCheckInterval = null;
let failedChecks = 0;
const MAX_FAILED_CHECKS = 3;

function startConnectionCheck() {
  if (connectionCheckInterval) clearInterval(connectionCheckInterval);

  connectionCheckInterval = setInterval(async () => {
    try {
      const res = await fetch('/api/status');
      const data = await res.json();

      if (data.connected) {
        failedChecks = 0;
        updateStatus(true);
      } else {
        failedChecks++;
        if (failedChecks >= MAX_FAILED_CHECKS) {
          updateStatus(false, data.message || 'Mehrere Checks fehlgeschlagen');
        }
      }
    } catch (err) {
      failedChecks++;
      if (failedChecks >= MAX_FAILED_CHECKS) {
        updateStatus(false, 'Keine Antwort');
      }
    }
  }, 6000);
}

window.startConnectionCheck = startConnectionCheck;