import * as THREE from 'three';

import * as Types from "../../../../types/world/worldTypes";

function getClosestIntersection(
    intersects: Types.Vector3Array,
    point: Types.Vector3
): Types.Vector3 | null {

    //////////  A function that finds which point is closest from the intersects points that is given, Used in finding which point in a line is closest when clicked on a line during drawing  ////////// 

    let closestNewPoint: THREE.Vector3 | null = null;
    let minDistance = Infinity;

    for (const intersect of intersects) {
        const distance = point.distanceTo(intersect);
        if (distance < minDistance) {
            minDistance = distance;
            closestNewPoint = intersect;
        }
    }

    return closestNewPoint;
}

export default getClosestIntersection;
