import { useEffect } from "react";
const useDrawLines = (setLinePositions) => {
  // Linien auf die Karte zeichnen
  useEffect(() => {
    const endpoint = "/api/talas_v5_DB/gisLines/readGisLines";
    //const endpoint = "http://localhost/talas5/ClientData/WebServiceMap.asmx/GisLinesStatus?idMap=10";
    fetch(endpoint)
      .then((response) => {
        if (!response.ok) {
          throw new Error(`HTTP error! status: ${response.status}`);
        }
        return response.json();
      })
      .then((data) => {
        const newLinePositions = data.map((item) => {
          //console.log("item.idLD", item.idLD);
          //console.log("item.idModul", item.idModul);

          if (item.points && Array.isArray(item.points)) {
            return {
              coordinates: item.points.map((point) => [point.x, point.y]),
              idModul: item.idModul,
              idLD: item.idLD,
            };
          } else {
            throw new Error("Points missing or not an array");
          }
        });
        setLinePositions(newLinePositions);
      })
      .catch((error) => {
        console.error("Error fetching data:", error.message);
      });
  }, []);
};

export default useDrawLines;
