import * as Cesium from "cesium";

const getCenterPosition = (positions) => {
  if(positions.length === 0) {
    console.warn("positions is empty");
    return Cesium.Cartesian3.ZERO;
  }
  
  let maxX = Cesium.Math.toRadians(-180),
    minX = Cesium.Math.toRadians(180);
  let maxY = Cesium.Math.toRadians(-90),
    minY = Cesium.Math.toRadians(90);

  for (let i = 0; i < positions.length; i++) {
    const cartographic = Cesium.Cartographic.fromCartesian(positions[i]);
    if (cartographic.longitude < minX) {
      minX = cartographic.longitude;
    } else if (cartographic.longitude > maxX) {
      maxX = cartographic.longitude;
    }
    if (cartographic.latitude < minY) {
      minY = cartographic.latitude;
    } else if (cartographic.latitude > maxY) {
      maxY = cartographic.latitude;
    }
  }
  return Cesium.Cartesian3.fromRadians((minX + maxX) / 2, (minY + maxY) / 2)
};

export {
  getCenterPosition
}
