// utils/handlePoiSelect.js
const handlePoiSelect = async (poiData, setSelectedPoi, setLocationDeviceData, setDeviceName, poiLayerRef, poiTypMap) => {
  setSelectedPoi(poiData); // Setzt das ausgewählte POI

  try {
    const response = await fetch("/api/talas_v5_DB/locationDevice/locationDevices");
    const data = await response.json();
    setLocationDeviceData(data);

    const currentDevice = data.find((device) => device.idLD === poiData.deviceId);
    if (currentDevice) {
      setDeviceName(currentDevice.name);

      // Hier speichern wir den POI-Typ im localStorage
      const poiTypeName = poiTypMap.get(poiData.idPoiTyp);
      localStorage.setItem("selectedPoiType", poiTypeName);

      // Optional: Update des Markers mit dem POI-Typ
      const marker = poiLayerRef.current.getLayers().find((m) => m.options.id === poiData.id);
      if (marker) {
        marker.setPopupContent(`
          <div>
            <b class="text-xl text-black-700">${poiData.description || "Unbekannt"}</b><br>
            ${currentDevice.name}<br>
            ${poiTypeName || "Unbekannt"}<br>
          </div>
        `);
        marker.openPopup();
      }
    }
  } catch (error) {
    console.error("Fehler beim Abrufen der Gerätedaten:", error);
    setLocationDeviceData([]);
  }
};

export default handlePoiSelect;
