import { system, world } from "@minecraft/server";
import { SettingsHandler } from "./settings";
let heallingSpongeMap = new Map();
const scheduledParticles = [];
export function gravitySpongePulse(block) {
    // Check if gravity sponge is enabled
    if (!SettingsHandler.isSpongeEnabled("gravitySponge")) {
        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("§cGravity Sponge is disabled in settings!");
        }
        return;
    }
    const blockId = `${block.location.x}_${block.location.y}_${block.location.z}`;
    if (!heallingSpongeMap.has(blockId)) {
        heallingSpongeMap.set(blockId, Date.now() + (45 * 1000));
    }
    else {
        const time = heallingSpongeMap.get(blockId);
        if (time != undefined && time < Date.now()) {
            heallingSpongeMap.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.addEffect("minecraft:slow_falling", 110, { amplifier: 1 });
        player.addEffect("minecraft:jump_boost", 110, { amplifier: 5 });
    }
    world.getDimension(block.dimension.id).playSound("beacon.activate", block.location);
    world.getDimension(block.dimension.id).spawnParticle("wildphire_spg:gravity_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:gravity_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:gravity_ring_sponge_particle", { x: particle.location.x + 0.5, y: particle.location.y, z: particle.location.z + 0.5 });
        scheduledParticles.shift();
    }
}, 20);
