import WKT from "ol/format/WKT.js";
import Feature from 'ol/Feature.js';

export function cleanArrayUndefined(arr) {
    let nr = [];
    arr.forEach((v) => {
        if (v) {
            nr.push(v);
        }
    });
    return nr;
}

export function getValues(arr, key) {
    let ids = [];
    arr.forEach((item) => {
        ids.push(item[key]);
    });
    return ids;
}

export function getMethodValues(arr, method, key) {
    let ids = [];
    arr.forEach((item) => {
        ids.push(item[method](key));
    });
    return ids;
}

export const pointToFormat = (pointString) => {
    let geom = new WKT().readGeometry(pointString);
    const longitude = geom.getCoordinates()[0];
    const latitude = geom.getCoordinates()[1];
    // 转换为度的格式
    const formattedLongitude = `${longitude.toFixed(0)}°E`;
    const formattedLatitude = `${latitude.toFixed(0)}°N`;

    const result = `${formattedLongitude},${formattedLatitude}`;
    return result;
};

export const newAreaFeature = (data)=>{
    let geom = new WKT().readGeometry(data["geom"])
    let feature = new Feature({
        geometry: geom
    });
    feature.set("nodeType","area");
    feature.setId(data.id)
    for(let key in data){
        if(key != "geom"){
            feature.set(key,data[key])
        }
    }
    return feature;
}

export function formatToTwoDecimalPlaces(value) {
    if (typeof value === 'number') {
        // 检查是否为整数
        if (Number.isInteger(value)) {
            return value;
        } else {
            return value.toFixed(2);
        }
    } else {
        return value;
    }
}

