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

function removeSoloPoint(
    line: Types.RefLine,
    floorPlanGroupLine: Types.RefGroup,
    floorPlanGroupPoint: Types.RefGroup
): void {

    //////////  Remove the point if there is only one point and if it is not connected to any other line and also the reference line  //////////

    if (line.current[0]) {
        const pointUUID = line.current[0][1];
        let isConnected = false;

        floorPlanGroupLine.current.children.forEach((line) => {
            const linePoints = line.userData.linePoints;
            const uuid1 = linePoints[0][1];
            const uuid2 = linePoints[1][1];
            if (uuid1 === pointUUID || uuid2 === pointUUID) {
                isConnected = true;
            }
        });

        if (!isConnected) {
            floorPlanGroupPoint.current.children.forEach((point: any) => {
                if (point.uuid === pointUUID) {
                    (<any>point.material).dispose();
                    (<any>point.geometry).dispose();
                    floorPlanGroupPoint.current.remove(point);
                }
            });
        }
        line.current = [];
    }
}

export default removeSoloPoint;
