import { Block, world } from "@minecraft/server";

/**
 * Recursively finds and breaks all connected log blocks.
 * 
 * @param {Block} block The initial block hit by the player.
 */
export function getLogsAndBreak(block) {
    const dimension = block.dimension;
    const queue = [block.location];
    const visited = new Set();

    while (queue.length > 0) {
        const currentLocation = queue.shift();
        const currentKey = `${currentLocation.x},${currentLocation.y},${currentLocation.z}`;

        if (visited.has(currentKey)) {
            continue;
        }

        visited.add(currentKey);
        const currentBlock = dimension.getBlock(currentLocation);

        if (validTreeBlock(currentBlock)) {
            try {
                dimension.runCommandAsync(`/fill ${currentLocation.x} ${currentLocation.y} ${currentLocation.z} ${currentLocation.x} ${currentLocation.y} ${currentLocation.z} air destroy`);
            } catch (error) {
                return
            }

            // Check all adjacent blocks (6 directions)
            const directions = [
                currentBlock.above(),
                currentBlock.below(),
                currentBlock.north(),
                currentBlock.south(),
                currentBlock.east(),
                currentBlock.west(),
                // diagonals
                currentBlock.above().north(),
                currentBlock.above().south(),
                currentBlock.above().east(),
                currentBlock.above().west(),
                currentBlock.below().north(),
                currentBlock.below().south(),
                currentBlock.below().east(),
                currentBlock.below().west(),
                currentBlock.north().east(),
                currentBlock.north().west(),
                currentBlock.south().east(),
                currentBlock.south().west(),
                // corner diagonals
                currentBlock.above().north().east(),
                currentBlock.above().north().west(),
                currentBlock.above().south().east(),
                currentBlock.above().south().west(),
                currentBlock.below().north().east(),
                currentBlock.below().north().west(),
                currentBlock.below().south().east(),
                currentBlock.below().south().west()
            ];

            directions.forEach(direction => {
                const neighborLocation = direction.location;
                if (!visited.has(`${neighborLocation.x},${neighborLocation.y},${neighborLocation.z}`)) {
                    const neighborBlock = dimension.getBlock(neighborLocation);
                    if (validTreeBlock(neighborBlock)) {
                        queue.push(neighborLocation);
                    }
                }
            });
        }
    }
}

/**
 * 
 * @param {Block} block 
 * @returns {boolean}
 */
function isLeaf(block) {
    return block.permutation.matches("acacia_leaves") || block.permutation.matches("azalea_leaves") || block.permutation.matches("azalea_leaves_flowered") || block.permutation.matches("birch_leaves") || block.permutation.matches("cherry_leaves") || block.permutation.matches("dark_oak_leaves") || block.permutation.matches("jungle_leaves") || block.permutation.matches("mangrove_leaves") || block.permutation.matches("oak_leaves") || block.permutation.matches("spruce_leaves") || block.permutation.matches("warped_wart_block") || block.permutation.matches("nether_wart_block");
}

export function validTreeBlock(block) {
    return block.hasTag("log") || block.permutation.matches("crimson_stem") || block.permutation.matches("warped_stem") || isLeaf(block) || block.typeId.includes("wood") || block.typeId.includes("cherry_log");
}
