import { audioPath } from "./path.js";

let pipe = document.getElementById("meow");
let audio = new Audio(`${audioPath}anvil_land_low.ogg`);
let hoverTimeout;
let hovered = false;
let played = localStorage.getItem("audioPlayed") === "true";
let preload = new Image();
preload.src = "/assets/img/button/braixdragon-cat2.gif";

window.addEventListener("beforeunload", () => {
	localStorage.removeItem("catflattened");
});

pipe.addEventListener(
	"mouseover",
	() => {
		if (played) return;
		hovered = true;
		hoverTimeout = setTimeout(() => {
			if (hovered && !played) {
				pipe.src = preload.src;
				setTimeout(() => {
					if (!played) {
						audio.play();
						played = true;
						localStorage.setItem("catflattened", "true");
					}
				}, 600);
			}
		}, 650);
	},
	true
);

pipe.addEventListener("mouseout", () => {
	hovered = false;
	clearTimeout(hoverTimeout);
});
