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

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

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

/**
 * @deprecated Use stores instead.
 */
export async function geocode(name: string) {
	const loc = saved[name];
	if (!loc) return;
	const geocode = await reverseGeocode(loc);
	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;
}
