import { lang } from "@/config";

export async function initializeSw() {
	if (!("serviceWorker" in navigator)) return;
	if (!window.isSecureContext) return;
	if (["localhost", "127.0.0.1"].includes(window.location.hostname)) return;

	try {
		await navigator.serviceWorker.register("/sw.js", {
			scope: "/",
			type: "classic",
		});
		const registration = await navigator.serviceWorker.ready;
		registration.active?.postMessage({
			msg: "initialize",
			lang,
		});
	} catch (error) {
		console.error("Failed to initialize service worker", error);
	}
}
