import { world, system, MolangVariableMap } from "@minecraft/server";
const blockParticleMap = {
    "lv_zo:corrupted_leaves": "minecraft:biome_tinted_leaves_particle",
    "lv_zo:corrupted_bush": "lv_zo:bush_emitter",
    "lv_zo:corrupted_flower": "lv_zo:ash_emitter",
    "lv_zo:corrupted_cave_hanging_plant": "lv_zo:cave_root_emitter",
    "lv_zo:corrupted_cave_light": "lv_zo:corrupted_cave_light_emitter"
};

system.runInterval(() => {
    try {
        const players = world.getPlayers();
        for (const player of players) {
            const origin = player.location;
            const radius = 12; // Horizontal search radius
            const verticalRadius = 10; // Vertical search radius

            // Check VFX toggle
            let vfxEnabled = true;
            try {
                const objective = world.scoreboard.getObjective("lv_zo_global_toggles");
                const participant = objective?.getParticipants().find(p => p.displayName === "toggle_vfx");
                if (participant) {
                    const score = objective.getScore(participant);
                    vfxEnabled = score === 1;
                }
            } catch {
                vfxEnabled = true; // fail-safe default
            }

            if (!vfxEnabled) continue;

            for (let x = -radius; x <= radius; x++) {
                for (let y = -verticalRadius; y <= verticalRadius; y++) {
                    for (let z = -radius; z <= radius; z++) {
                        if (Math.random() > 0.3) continue; // 70% skip

                        const pos = {
                            x: Math.floor(origin.x) + x,
                            y: Math.floor(origin.y) + y,
                            z: Math.floor(origin.z) + z,
                        };

                        const block = player.dimension.getBlock(pos);
                        if (!block) continue;

                        const particleId = blockParticleMap[block.typeId];
                        if (!particleId) continue;

                        if (Math.random() < 0.2) { // 20% spawn chance
                            const spawnPos = {
                                x: pos.x,
                                y: pos.y,
                                z: pos.z
                            };

                            if (particleId === "minecraft:biome_tinted_leaves_particle") {
                                if (Math.random() < 0.08) { // 8% spawn chance
                                    // Special spawn for colored leaves
                                    const variables = new MolangVariableMap();
                                    variables.setColorRGB("variable.color", {
                                        red: 0.541,
                                        green: 0.157,
                                        blue: 0.125
                                    });

                                    player.dimension.spawnParticle(
                                        particleId,
                                        spawnPos,
                                        variables
                                    );
                                }
                            } else {
                                // Normal spawn
                                player.dimension.spawnParticle(
                                    particleId,
                                    spawnPos
                                );
                            }
                        }
                    }
                }
            }
        }
    } catch (error) {

    }

}, 5); // Runs every tick, controlled internally
