import { BlockPermutation, WeatherType, system, world } from "@minecraft/server";
import { SettingsHandler } from "./settings";
// Track if a clear weather sponge is active
let isSpongeActive = false;
export function clearWeatherSpongePlace(block, player) {
    system.run(() => {
        try {
            // Check if clear weather sponge is enabled
            if (!SettingsHandler.isSpongeEnabled("clearWeatherSponge")) {
                block.dimension.getBlock(block.location)?.setPermutation(BlockPermutation.resolve("minecraft:air"));
                player.sendMessage("§cClear Weather Sponge is disabled in settings!");
                return;
            }
            // If another sponge is active, break the new one and return
            if (isSpongeActive) {
                block.dimension.getBlock(block.location)?.setPermutation(BlockPermutation.resolve("minecraft:air"));
                player.onScreenDisplay.setActionBar("§cA Clear Weather Sponge is already active!");
                return;
            }
            // Set weather to clear
            block.dimension.setWeather(WeatherType.Clear);
            // Spawn particle effect at the block's location
            // Mark as active
            isSpongeActive = true;
            // Wait 4 seconds (80 ticks), then remove the sponge, drop item, and reset state
            block.dimension.spawnParticle("wildphire_spg:time_set_day_particle", {
                x: block.location.x + 0.5,
                y: block.location.y + 0.5,
                z: block.location.z + 0.5
            });
            //beacon powerup sound
            block.dimension.playSound("beacon.activate", {
                x: block.location.x + 0.5,
                y: block.location.y + 0.5,
                z: block.location.z + 0.5
            });
            system.runTimeout(() => {
                // Drop the clear weather sponge item at the block's location
                world.getDimension(block.dimension.id).runCommand(`setblock ${block.location.x} ${block.location.y} ${block.location.z} air destroy`);
                isSpongeActive = false;
            }, 40);
        }
        catch (e) {
        }
    });
}
