import { Vector3 } from 'three';
import { traits } from './ecs';
const hoverPosition = new Vector3();
export const getPointAtIndex = (positions, index) => {
    if (index < 0 || index >= positions.length / 3) {
        return null;
    }
    return {
        index,
        x: positions[index * 3],
        y: positions[index * 3 + 1],
        z: positions[index * 3 + 2],
        oX: 0,
        oY: 0,
        oZ: 0,
        theta: 0,
    };
};
export const getArrowAtIndex = (positions, index) => {
    if (index < 0 || index >= positions.length / 6) {
        return null;
    }
    return {
        index,
        x: positions[index * 6] / 1000,
        y: positions[index * 6 + 1] / 1000,
        z: positions[index * 6 + 2] / 1000,
        oX: positions[index * 6 + 3],
        oY: positions[index * 6 + 4],
        oZ: positions[index * 6 + 5],
        theta: 0,
    };
};
export const updateHoverInfo = (entity, hoverEvent) => {
    const { index, point } = hoverEvent;
    if (index === -1) {
        return null;
    }
    hoverPosition.set(point.x, point.y, point.z);
    let hoverInfo = null;
    if (entity.has(traits.Arrows)) {
        let closestArrow = null;
        if (index && index > 0) {
            closestArrow = getArrowAtIndex(entity.get(traits.Positions), index);
        }
        if (closestArrow) {
            hoverInfo = closestArrow;
        }
    }
    else if (entity.has(traits.Points)) {
        const positions = entity.get(traits.BufferGeometry)?.attributes.position.array;
        let closestPoint = null;
        if (index && index > 0) {
            closestPoint = getPointAtIndex(positions, index);
        }
        if (closestPoint) {
            hoverInfo = closestPoint;
        }
    }
    return hoverInfo;
};
export const getLinkedHoverInfo = (index, linkedEntity) => {
    if (linkedEntity.has(traits.Arrows)) {
        const closestArrow = getArrowAtIndex(linkedEntity.get(traits.Positions), index);
        if (closestArrow) {
            return closestArrow;
        }
    }
    else if (linkedEntity.has(traits.Points)) {
        const positions = linkedEntity.get(traits.BufferGeometry)?.attributes.position
            .array;
        const closestPoint = getPointAtIndex(positions, index);
        if (closestPoint) {
            return closestPoint;
        }
    }
    return null;
};
