import { blockNeighbors, checkAnyNeighborHasTag } from "../../library/blockManager";

/**
 * Evento del componente repeaterCableSignalComponent
 * @param {{block: Block, dimension: Dimension}} event 
 */
export const repeaterCableSignalEvent = ({ block, dimension }) => {
    try {
        if(!block) return;
        const {
            blockNeighborUp, blockNeighborDown,
            blockNeighborEast, blockNeighborWest,
            blockNeighborNorth, blockNeighborSouth
        } = blockNeighbors(block);

        if(!blockNeighborUp || !blockNeighborDown ||
            !blockNeighborEast || !blockNeighborWest ||
            !blockNeighborNorth || !blockNeighborSouth) return;


        let permutation = block.permutation;

        if(block.typeId === 'minecraft:air') return

        // Verificar y establecer el estado de energía
        if (permutation.getState('nps_ec:power') === 0) {
            const condition = checkAnyNeighborHasTag(block, 'power:pure:15');
            if (condition) {
                permutation = permutation.withState('nps_ec:power', 2);
            } else {
                const condition = checkAnyNeighborHasTag(block, 'power:15');
                if (condition) {
                    permutation = permutation.withState('nps_ec:power', 1);
                }
            }
        } else {
            if(permutation.getState('nps_ec:power') === 1) {
                const condition = !checkAnyNeighborHasTag(block, 'power:15');
                if (condition) {
                    permutation = permutation.withState('nps_ec:power', 0);
                }
            } else {
                const condition = !checkAnyNeighborHasTag(block, 'power:pure:15');
                if (condition) {
                    permutation = permutation.withState('nps_ec:power', 0);
                }
            }
        }

        block.setPermutation(permutation);
    } catch { }
}

/*
{ //repeater
"condition": "query.block_state('nps_ec:power') == 0",
"sequence": [
    {
        "condition": "query.block_state('nps_ec:power') != 2",
        "sequence": [
            {
                "condition": "query.block_neighbor_has_any_tag(0,1,0,'power:15') || query.block_neighbor_has_any_tag(0,-1,0,'power:15') || query.block_neighbor_has_any_tag(1,0,0,'power:15') || query.block_neighbor_has_any_tag(-1,0,0,'power:15') || query.block_neighbor_has_any_tag(0,0,-1,'power:15') || query.block_neighbor_has_any_tag(0,0,1,'power:15')",
                "set_block_property": {
                    "nps_ec:power": 1
                }
            }
        ]
    },
    {
        "condition": "query.block_state('nps_ec:power') != 1",
        "sequence": [
            {
                "condition": "query.block_neighbor_has_any_tag(0,1,0,'power:pure:15') || query.block_neighbor_has_any_tag(0,-1,0,'power:pure:15') || query.block_neighbor_has_any_tag(1,0,0,'power:pure:15') || query.block_neighbor_has_any_tag(-1,0,0,'power:pure:15') || query.block_neighbor_has_any_tag(0,0,-1,'power:pure:15') || query.block_neighbor_has_any_tag(0,0,1,'power:pure:15')",
                "set_block_property": {
                    "nps_ec:power": 2
                }
            }
        ]
    }
]
},
{
"condition": "query.block_state('nps_ec:power') == 1",
"sequence": [
    {
        "condition": "(!query.block_neighbor_has_any_tag(0,1,0,'power:15')) && (!query.block_neighbor_has_any_tag(0,-1,0,'power:15')) && (!query.block_neighbor_has_any_tag(1,0,0,'power:15')) && (!query.block_neighbor_has_any_tag(-1,0,0,'power:15')) && (!query.block_neighbor_has_any_tag(0,0,-1,'power:15')) && (!query.block_neighbor_has_any_tag(0,0,1,'power:15'))",
        "set_block_property": {
            "nps_ec:power": 0
        }
    }
]
},
{
"condition": "query.block_state('nps_ec:power') == 2",
"sequence": [
    {
        "condition": "(!query.block_neighbor_has_any_tag(0,1,0,'power:pure:15')) && (!query.block_neighbor_has_any_tag(0,-1,0,'power:pure:15')) && (!query.block_neighbor_has_any_tag(1,0,0,'power:pure:15')) && (!query.block_neighbor_has_any_tag(-1,0,0,'power:pure:15')) && (!query.block_neighbor_has_any_tag(0,0,-1,'power:pure:15')) && (!query.block_neighbor_has_any_tag(0,0,1,'power:pure:15'))",
        "set_block_property": {
            "nps_ec:power": 0
        }
    }
]
}
*/