export type SavedPerimeter = {
  name: string;
  polygon: [number, number][];
  createdAt: string;
};

const STORAGE_KEY = 'gw-saved-perimeters';

export function useSavedPerimeters() {
  const perimeters = useLocalStorage<SavedPerimeter[]>(STORAGE_KEY, []);

  function savePerimeter(name: string, polygon: [number, number][]) {
    perimeters.value = [
      ...perimeters.value.filter((p) => p.name !== name),
      { name, polygon, createdAt: new Date().toISOString() },
    ];
  }

  function deletePerimeter(name: string) {
    perimeters.value = perimeters.value.filter((p) => p.name !== name);
  }

  return { perimeters, savePerimeter, deletePerimeter };
}
