/* eslint-disable no-console */

import { register } from "register-service-worker";

// Check if we're in an Electron environment
const isElectron =
  process.env.VITE_PLATFORM === "electron" ||
  process.env.VITE_DISABLE_PWA === "true" ||
  window.navigator.userAgent.toLowerCase().includes("electron");

// Only register service worker if:
// 1. Not in Electron
// 2. PWA is explicitly enabled
// 3. In production mode
if (
  !isElectron &&
  process.env.VITE_PWA_ENABLED === "true" &&
  process.env.NODE_ENV === "production"
) {
  register(`${process.env.BASE_URL}sw.js`, {
    ready() {
      console.log("Service worker is active.");
    },
    registered() {
      console.log("Service worker has been registered.");
    },
    cached() {
      console.log("Content has been cached for offline use.");
    },
    updatefound() {
      console.log("New content is downloading.");
    },
    updated() {
      console.log("New content is available; please refresh.");
    },
    offline() {
      console.log(
        "No internet connection found. App is running in offline mode.",
      );
    },
    error(error) {
      console.error("Error during service worker registration:", error);
    },
  });
} else {
  console.log(
    `Service worker registration skipped - ${
      isElectron
        ? "running in Electron"
        : process.env.VITE_PWA_ENABLED !== "true"
          ? "PWA not enabled"
          : "not in production mode"
    }`,
  );
}
