// Global ATON

import AppState from "../state.js";

/**
 * @module AtonUtils
 */
/**
 * Initializes required ATON events for interactions
 * with semantic nodes
 */
export function initAtonEvents() {
    // General hover/leave events handling for all semantic nodes.
    ATON.on("SemanticNodeHover", (semid) => {
        let node = ATON.getSemanticNode(semid);
        if (node) node.highlight();
    });

    ATON.on("SemanticNodeLeave", (semid) => {
        let node = ATON.getSemanticNode(semid);
        if (node) node.restoreDefaultMaterial();
    });

    // Triggers on mouse click / tap events
    ATON.on("Tap", (e) => {
        let node = ATON.getHoveredSemanticNode();
        if (node) {
            // Retrieve semantic node from AppState to trigger a modal with its content
            const content = AppState.semanticNodes.get(node.nid);
            
            // Triggers a modal to show the content (listened by Stimulus)
            const event = new Event('semantic-modal-show');
            event.content = content;

            document.dispatchEvent(event);
        }
    });
}