import { getHazards, hasCapability, type Hazard } from "$lib/services/lnv";
import { location } from "./location.svelte";

export const hazards: Hazard[] = $state([]);

export async function fetchHazards() {
	if (!location.available) return;
	if (!(await hasCapability("hazards"))) return;
	const newHazards = await getHazards(
		{ lat: location.lat, lon: location.lng },
		100,
	); // TODO: get radius from server config
	hazards.splice(0, hazards.length, ...newHazards);
}

setInterval(
	() => {
		fetchHazards();
	},
	1000 * 60 * 5,
); // Every 5 minutes
// TODO: get from server config

const initalFetch = setInterval(() => {
	if (location.available) {
		fetchHazards();
		clearInterval(initalFetch);
	}
});
