import { Block, Dimension } from "@minecraft/server";
import { blockNeighbors, checkAnyNeighborHasTag, setBlockProperty } from "../../library/blockManager";

/**
 * Evento Tick del componente flashLightComponent
 * @param {{block: Block, dimension: Dimension}} event 
 */
export const flashLightTickEvent = ({block, dimension}) => {
    try {
        const {
            blockNeighborDown, blockNeighborUp,
            blockNeighborEast, blockNeighborWest,
            blockNeighborNorth, blockNeighborSouth
        } = blockNeighbors(block);
    
        // Verificar que todos los vecinos existan
        if (!blockNeighborUp || !blockNeighborDown ||
            !blockNeighborEast || !blockNeighborWest ||
            !blockNeighborNorth || !blockNeighborSouth) return;
    
        const powerState = block.permutation.getState('nps_ec:power');
    
        // Actualizar el estado de energía basado en las condiciones
        if (powerState === 0) {
            if (checkAnyNeighborHasTag(block, 'power:on')) {
                setBlockProperty(block, 'nps_ec:power', 1);
            } else if (checkAnyNeighborHasTag(block, 'power:light')) {
                setBlockProperty(block, 'nps_ec:power', 2);
            }
        } else if (powerState === 1) {
            if (!checkAnyNeighborHasTag(block, 'power:on')) {
                setBlockProperty(block, 'nps_ec:power', 0);
            }
        } else if (powerState === 2) {
            if (!checkAnyNeighborHasTag(block, 'power:light')) {
                setBlockProperty(block, 'nps_ec:power', 0);
            }
        }
    } catch { }
}