import * as THREE from 'three';

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

function DeletableHoveredFloorItems(
    state: Types.ThreeState,
    itemsGroup: Types.RefGroup,
    hoveredDeletableFloorItem: Types.RefMesh,
    setDeletableFloorItem: any
): void {

    ////////// Altering the color of the hovered GLTF item during the Deletion time  //////////

    state.raycaster.setFromCamera(state.pointer, state.camera);
    const intersects = state.raycaster.intersectObjects(itemsGroup.current.children, true);

    if (intersects.length > 0) {
        if (intersects[0].object.name === "Pole") {
            return;
        }
        if (hoveredDeletableFloorItem.current) {
            hoveredDeletableFloorItem.current = undefined;
            setDeletableFloorItem(null);
        }
        let currentObject = intersects[0].object;

        while (currentObject) {
            if (currentObject.name === "Scene") {
                hoveredDeletableFloorItem.current = currentObject as THREE.Mesh;
                setDeletableFloorItem(currentObject);
                break;
            }
            currentObject = currentObject.parent as THREE.Object3D;
        }
    } else {
        if (hoveredDeletableFloorItem.current) {
            hoveredDeletableFloorItem.current = undefined;
            setDeletableFloorItem(null);
        }
    }
}

export default DeletableHoveredFloorItems;
