import { Block, Player } from "@minecraft/server";
import { namespace } from "../../namespace";

/**
 * 
 * @param {Block} block
 * @param {Player} player
 */
export function veinMine(block, player) {
    if (player.getDynamicProperty(`${namespace}:in_tutorial`) == true) {
        // check if block is an ore
        
    }
    const dimension = block.dimension;
    const queue = [block.location];
    const visited = new Set();
    let counter = 0
    const max = 40

    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 (currentBlock.typeId == block.typeId || (block.typeId == "minecraft:lit_redstone_ore" && currentBlock.typeId == "minecraft:redstone_ore") || (block.typeId == "minecraft:lit_deepslate_redstone_ore" && currentBlock.typeId == "minecraft:deepslate_redstone_ore")) {
            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()
            ];

            directions.forEach(direction => {
                const neighborLocation = direction.location;
                if (!visited.has(`${neighborLocation.x},${neighborLocation.y},${neighborLocation.z}`)) {
                    const neighborBlock = dimension.getBlock(neighborLocation);
                    if (neighborBlock.typeId == block.typeId || (block.typeId == "minecraft:lit_redstone_ore" && neighborBlock.typeId == "minecraft:redstone_ore") || (block.typeId == "minecraft:lit_deepslate_redstone_ore" && neighborBlock.typeId == "minecraft:deepslate_redstone_ore")) {
                        queue.push(neighborLocation);
                    }
                }
            });
        }
    }
}

