// utils/redrawPolyline.js
export const redrawPolyline = (lineData, lineColors, tooltipContents, map) => {
  if (!lineData || !lineColors || !tooltipContents || !map) {
    console.error("Invalid parameters for redrawPolyline");
    return;
  }

  if (!lineData.coordinates || !Array.isArray(lineData.coordinates)) {
    console.error("Invalid coordinates in lineData");
    return;
  }

  const color = lineColors[lineData.idModul] || "#000000";
  const tooltipContent = tooltipContents[lineData.idModul] || "Standard-Tooltip-Inhalt";

  if (lineData.polyline) map.removeLayer(lineData.polyline);

  lineData.polyline = L.polyline(lineData.coordinates, {
    color: color,
  }).addTo(map);

  lineData.polyline.bindTooltip(tooltipContent, {
    permanent: false,
    direction: "auto",
  });

  lineData.polyline.on("mouseover", () => {
    lineData.polyline.setStyle({ weight: 10 });
    lineData.polyline.bringToFront();
  });

  lineData.polyline.on("mouseout", () => {
    lineData.polyline.setStyle({ weight: 5 });
  });
};
