const LOCAL_STORAGE_KEY = "bungee";

export function trackOnceIfNotAlready({
	type,
	requestHash,
	trackFn,
}: {
	type: "manual" | "auto";
	requestHash: string;
	trackFn: () => void;
}) {
	if (typeof window === "undefined") return;

	const raw = localStorage.getItem(LOCAL_STORAGE_KEY);
	const data = raw ? JSON.parse(raw) : {};

	// Setup structure if missing
	data.tracked = data.tracked || {};
	data.tracked[type] = data.tracked[type] || {};

	if (!data.tracked[type][requestHash]) {
		trackFn(); // Run tracking callback
		data.tracked[type][requestHash] = true;
		localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(data));
	}
}
