const CaveRoot = {
    onPlace(event) {
        let block = event.block; // The block that was placed
        let dimension = event.dimension; // Dimension that contains the block

        // Get the block ABOVE the placed block
        const aboveBlock = dimension.getBlock({ x: block.location.x, y: block.location.y + 1, z: block.location.z });

        if (aboveBlock) {
            if (aboveBlock.typeId === "lv_zo:corrupted_cave_hanging_root") {
                // Turn it into corrupted_cave_hanging_plant
                aboveBlock.setType("lv_zo:corrupted_cave_hanging_plant");
                block.setType("minecraft:air");
            } else if (aboveBlock.typeId === "lv_zo:corrupted_cave_hanging_plant") {
                // Get the block ABOVE the hanging plant
                const aboveAboveBlock = dimension.getBlock({ x: block.location.x, y: block.location.y + 1, z: block.location.z });
                if (aboveAboveBlock) {
                    block.setType("lv_zo:corrupted_cave_hanging_plant_extra");
                }
            }
        }
    },

    onTick(event) {
        let block = event.block;
        let dimension = event.dimension;
        // Only grow if this block is a hanging plant or hanging plant extra
        if (
            block.typeId === "lv_zo:corrupted_cave_hanging_plant" ||
            block.typeId === "lv_zo:corrupted_cave_hanging_plant_extra"
        ) {
            const belowBlock = dimension.getBlock({ x: block.location.x, y: block.location.y - 1, z: block.location.z });

            if (belowBlock && belowBlock.typeId === "minecraft:air") {
                // Decide what to grow:
                // - If current block is "corrupted_cave_hanging_plant", grow an "extra"
                // - If already "extra", grow another "extra" downward
                belowBlock.setType("lv_zo:corrupted_cave_hanging_plant_extra");
            }
        }
        // Only grow if this block is a hanging plant or hanging plant extra
        if (
            block.typeId === "lv_zo:corrupted_cave_hanging_root"
        ) {

            block.setType("lv_zo:corrupted_cave_hanging_plant");
        }
    }
};

export default CaveRoot;
