import { system, world } from "@minecraft/server";
import { SettingsHandler } from "./settings";
let witherSpongeMap = new Map();
const scheduledParticles = [];
export function witherSpongePulse(block) {
    // Check if wither sponge is enabled
    if (!SettingsHandler.isSpongeEnabled("witherSponge")) {
        world.getDimension(block.dimension.id).runCommand(`setblock ${block.location.x} ${block.location.y} ${block.location.z} air destroy`);
        // Notify nearby players
        const nearbyPlayers = world.getDimension(block.dimension.id).getPlayers({ location: block.location, maxDistance: 10 });
        for (const player of nearbyPlayers) {
            player.sendMessage("§cWither Sponge is disabled in settings!");
        }
        return;
    }
    const blockId = `${block.location.x}_${block.location.y}_${block.location.z}`;
    if (!witherSpongeMap.has(blockId)) {
        witherSpongeMap.set(blockId, Date.now() + (45 * 1000));
    }
    else {
        const time = witherSpongeMap.get(blockId);
        if (time != undefined && time < Date.now()) {
            witherSpongeMap.delete(blockId);
            world.getDimension(block.dimension.id).runCommand(`setblock ${block.location.x} ${block.location.y} ${block.location.z} air destroy`);
            scheduledParticles.splice(0, scheduledParticles.length);
        }
    }
    const players = world.getDimension(block.dimension.id).getEntities({ maxDistance: 10, type: "minecraft:player", location: block.location });
    if (players.length == 0)
        return;
    for (const player of players) {
        player.removeEffect("minecraft:wither");
    }
    const withers = world.getDimension(block.dimension.id).getEntities({ type: "minecraft:wither_skeleton", maxDistance: 10, location: block.location });
    for (const wither of withers) {
        wither.applyDamage(1.5);
        world.getDimension(block.dimension.id).spawnParticle("wildphire_spg:wither_damage_sponge_particle", { x: wither.location.x + 0.5, y: wither.location.y, z: wither.location.z + 0.5 });
    }
    world.getDimension(block.dimension.id).playSound("entity.zombie.converted_to_drowned", block.location);
    world.getDimension(block.dimension.id).spawnParticle("wildphire_spg:wither_ring_sponge_particle", { x: block.location.x + 0.5, y: block.location.y, z: block.location.z + 0.5 });
    world.getDimension(block.dimension.id).spawnParticle("wildphire_spg:wither_sponge_particle", { x: block.location.x + 0.5, y: block.location.y, z: block.location.z + 0.5 });
    for (let i = 0; i <= 3; i++) {
        scheduledParticles.push({
            location: block.location,
            dimension: block.dimension
        });
    }
}
system.runInterval(() => {
    if (scheduledParticles.length > 0) {
        const particle = scheduledParticles[0];
        world.getDimension(particle.dimension.id).spawnParticle("wildphire_spg:wither_ring_sponge_particle", { x: particle.location.x + 0.5, y: particle.location.y, z: particle.location.z + 0.5 });
        scheduledParticles.shift();
    }
}, 20);
