import WKT from "terraformer-wkt-parser"
import { wktToGeoJSON,geojsonToWKT  } from "@terraformer/wkt"


const wktCollectionToGeoJson = (wktStr) => {
  console.log("wktStr",wktStr);
  let geojson = WKT.parse(wktStr);
  console.log("geojson",geojson);
}

const WktToGeojson = (wktData)=> {
    // return WKT.parse(wktData)
    console.log("wktData",wktData);
    return wktToGeoJSON(wktData);
}

const GeojsonToWkt = (geojsonData)=> {
    // return WKT.convert(geojsonData)
    console.log("geojsonData",geojsonData)
    return geojsonToWKT(geojsonData)
}


const  removeZM = (geoJSON) => {
    
    function removeZMFromCoords(coords) {
      return coords.map((coord) => [coord[0], coord[1]]); // 只保留 X 和 Y
    }
    
    switch (geoJSON.type) {
      case 'Point':
        geoJSON.coordinates = removeZMFromCoords([geoJSON.coordinates]).flat();
        break;
      case 'LineString':
           
      case 'MultiPoint':
        geoJSON.coordinates = removeZMFromCoords(geoJSON.coordinates);
        break;
      case 'Polygon':

      case 'MultiLineString':
        geoJSON.coordinates = geoJSON.coordinates.map((ring) => removeZMFromCoords(ring));
        break;
      case 'MultiPolygon':
        geoJSON.coordinates = geoJSON.coordinates.map((polygon) =>
          polygon.map((ring) => removeZMFromCoords(ring))
        );
        break;
      default:
        throw new Error(`Unsupported geometry type: ${geoJSON.type}`);
    }

    return geoJSON;

  }

export {WktToGeojson,GeojsonToWkt,removeZM,wktCollectionToGeoJson}