import React from "react";
import ReactDOM from "react-dom/client";
import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap-icons/font/bootstrap-icons.css";
import "./index.css";
import App from "./App";

// NUCLEAR OPTION: Completely kill ALL ResizeObserver errors
const errorHandler = (e) => {
  const msg = (e && e.message) || (e && e.reason && e.reason.message) || "";
  if (msg.includes("ResizeObserver") || msg.includes("loop")) {
    e.stopImmediatePropagation && e.stopImmediatePropagation();
    e.stopPropagation && e.stopPropagation();
    e.preventDefault && e.preventDefault();
    return false;
  }
};

const originalError = console.error;
console.error = (...args) => {
  const firstArg = args[0];
  if (
    firstArg &&
    typeof firstArg === "string" &&
    (firstArg.includes("ResizeObserver") || firstArg.includes("loop"))
  ) {
    return;
  }
  originalError.apply(console, args);
};

window.addEventListener("error", errorHandler, { capture: true });
window.addEventListener("unhandledrejection", errorHandler, { capture: true });

// Register Service Worker for caching
if ("serviceWorker" in navigator) {
  window.addEventListener("load", () => {
    navigator.serviceWorker
      .register("/service-worker.js")
      .then((registration) => {
        console.log("[App] Service Worker registered:", registration.scope);

        // Check for updates periodically
        setInterval(() => {
          registration.update();
        }, 60 * 60 * 1000); // Check every hour
      })
      .catch((error) => {
        console.log("[App] Service Worker registration failed:", error);
      });
  });
}

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);
