const __APP_VERSION__ = "__APP_VERSION";
const __APP_BUILD_TIME__ = __APP_BUILD_TIME;

import "./styles.css";

import Typed from "typed.js";

window.addEventListener("load", (_ev) => {
	new Typed(document.querySelector("description") ?? "description", {
		stringsElement: "#description-list",
		shuffle: true,
		typeSpeed: 60,
		backDelay: 2000,
		fadeOut: false,
		loop: true,
		showCursor: false,
	});

	document.querySelectorAll("[alt]").forEach((Elem) => {
		const tooltip = document.createElement("tooltiptext");
		tooltip.innerText = Elem.getAttribute("alt") ?? "";
		Elem.appendChild(tooltip);
	});

	// @ts-expect-error
	document.querySelector(
		"#footer > div"
	).innerHTML += `<a href="https://git.186526.xyz/186526/home-app"><img alt="Update Commit" src="https://blog.186526.xyz/shields.io/badge/commit-${__APP_VERSION__}-blue" /></a> <img alt="Latest Update" src="https://blog.186526.xyz/shields.io/date/${__APP_BUILD_TIME__}?color=sucessful&label=latest%20update" />`;

	if ("serviceWorker" in navigator) {
		navigator.serviceWorker
			.register("./sw.js", {
				scope: "/",
			})
			.then(function (registration) {
				console.log(
					"ServiceWorker registration successful with scope: ",
					registration.scope
				);
			})
			.catch(function (err) {
				console.warn("ServiceWorker registration failed: ", err);
			});
		let refreshing = false;
		navigator.serviceWorker.addEventListener("controllerchange", () => {
			if (refreshing) {
				return;
			}
			refreshing = true;
			window.location.reload();
		});
	}
});
