let url_Backend_dwinzo = `http://${process.env.REACT_APP_SERVER_REST_API_BASE_URL}`;
// let url_Backend_dwinzo = `http://192.168.0.102:5000`;
export const update3dWidget = async (
  zoneUuid: string,
  organization: string,
  id: string,
  position?: [number, number, number]
) => {

  try {
    const response = await fetch(
      `${url_Backend_dwinzo}/api/V1/widget3d/update`,
      {
        method: "PATCH",
        headers: {
          Authorization: "Bearer <access_token>",
          "Content-Type": "application/json",
          token: localStorage.getItem("token") || "",
          refresh_token: localStorage.getItem("refreshToken") || "",
        },
        body: JSON.stringify({
          organization,
          zoneUuid,
          id,
          position,
        }),
      }
    );
    const newAccessToken = response.headers.get("x-access-token");
    if (newAccessToken) {
      //console.log("New token received:", newAccessToken);
      localStorage.setItem("token", newAccessToken);
    }


    if (!response.ok) {
      echo.error("Failed to update 3d widget");
      console.error("Failed to add 3dwidget in the zone");
    }

    const result = await response.json();
    return result;
  } catch (error) {
    if (error instanceof Error) {
      console.log(error.message);
    } else {
      console.log("An unknown error occurred");
    }
  }
};

export const update3dWidgetRotation = async (
  zoneUuid: string,
  organization: string,
  id: string,
  rotation?: [number, number, number]
) => {
  try {
    const response = await fetch(
      `${url_Backend_dwinzo}/api/V1/widget3d/update`,
      {
        method: "PATCH",
        headers: {
          Authorization: "Bearer <access_token>",
          "Content-Type": "application/json",
          token: localStorage.getItem("token") || "",
          refresh_token: localStorage.getItem("refreshToken") || "",
        },
        body: JSON.stringify({
          organization,
          zoneUuid,
          id,
          rotation,
        }),
      }
    );

    if (!response.ok) {
      console.error("Failed to add 3dwidget in the zone");
    }

    const result = await response.json();
    return result;
  } catch (error) {
    echo.error("Failed to rotate 3d widget");
    if (error instanceof Error) {
      console.log(error.message);
    } else {
      console.log("An unknown error occurred");
    }
  }
};
