'use strict'

// This code is MIT licensed.
// Copyright Radost Waszkiewicz 2023

export function heightFromUTM(easting,northing) {
    let res = findNearestPoint(raw,[easting,northing]);
    let winner = res[0];
    let distance = res[1];
    return ""+winner+" (refrerence:"+(distance).toFixed(0)+"m away)";
}

function findNearestPoint(points, targetPoint) {
    // this function written by chat gpt
    let nearestDistance = Infinity;
    let nearestPoint = null;

    for (let i = 0; i < points.length; i++) {
        const [x, y, z] = points[i];
        const distance = Math.sqrt(Math.pow(x - targetPoint[0], 2) + Math.pow(y - targetPoint[1], 2));

        if (distance < nearestDistance) {
            nearestDistance = distance;
            nearestPoint = z;
        }
    }

    return [nearestPoint,nearestDistance];
}

let raw =