let url_Backend_dwinzo = `http://${process.env.REACT_APP_SERVER_REST_API_BASE_URL}`;
export const getZone2dData = async (organization?: string, projectId?: string, versionId?: string) => {
  try {
    const response = await fetch(
      `${url_Backend_dwinzo}/api/V1/zones/visualization/${projectId}/${versionId}`,
      {
        method: "GET",
        headers: {
          Authorization: `Bearer ${localStorage.getItem("token") || ""}`,
          "Content-Type": "application/json",
          token: localStorage.getItem("token") || "",
          refresh_token: localStorage.getItem("refreshToken") || "",
        },
      }
    );

    // Check and store new access token from headers (if any)
    const newAccessToken = response.headers.get("x-access-token");
    if (newAccessToken) {
      //console.log("New token received:", newAccessToken);
      localStorage.setItem("token", newAccessToken);
    }

    // Handle unauthorized response early
    if (response.status === 401) {
      return { status: 401 };
    }

    if (!response.ok) {
      throw new Error(`HTTP error! Status: ${response.status}`);
    }

    // Parse and return the actual JSON response
    return await response.json();
  } catch (error: any) {
    console.error("Failed to fetch 2D zone data:", error.message);
    return null;
  }
};

