import type { Component } from "svelte";
import { reverseGeocode } from "./services/Search";
import {
	BriefcaseIcon,
	HomeIcon,
	MapPinIcon,
	SchoolIcon,
	type IconProps,
} from "@lucide/svelte";
import { stores, storeSnapshot } from "./services/stores.svelte";

/**
 * @deprecated Use stores instead.
 */
export const saved: Record<string, WorldLocation> = $state(
	JSON.parse(localStorage.getItem("saved") ?? "{}"),
);

export const savedLocations = stores<Location>("location");

/**
 * @deprecated Use stores instead.
 */
export function saveLocations() {
	localStorage.setItem("saved", JSON.stringify(saved));
}

export async function geocode(name: string) {
	const loc = await storeSnapshot<Location>({ name, type: "location" });
	if (!loc) return;
	const geocode = await reverseGeocode({ lat: loc.lat, lon: loc.lng });
	if (geocode.length == 0) {
		return;
	}
	const feature = geocode[0];
	return `${feature.properties.street}${feature.properties.housenumber ? " " + feature.properties.housenumber : ""}, ${feature.properties.city}`;
}

export const ICONS: Record<string, Component<IconProps>> = {
	home: HomeIcon,
	work: BriefcaseIcon,
	school: SchoolIcon,
	pin: MapPinIcon,
};

export const MAP_ICONS: Record<string, string> = {
	home: "home.png",
	school: "school.png",
	work: "work.png",
}; // TODO: add generic pin icon

export interface Location {
	lat: number;
	lng: number;
	name: string;
	icon?: string;
}
