// /hooks/useMarkerLayers.js
import { useEffect } from "react";
import { useSelector } from "react-redux";
import { selectMapLayersState } from "../redux/slices/mapLayersSlice";

const useMarkerLayers = (map, markers, layerType) => {
  const mapLayersVisibility = useSelector(selectMapLayersState);

  useEffect(() => {
    if (!map || !markers) return;

    const toggleLayer = (isVisible) => {
      markers.forEach((marker) => {
        if (isVisible) {
          marker.addTo(map);
        } else {
          map.removeLayer(marker);
        }
      });
    };

    toggleLayer(mapLayersVisibility[layerType]);
  }, [map, markers, mapLayersVisibility, layerType]);
};

export default useMarkerLayers;
