import { world, BlockPermutation, system } from "@minecraft/server";
import { SettingsHandler } from "./settings";
let shadowSpongeMap = new Map();
const scheduledParticles = [];
export function shadowSpongePulse(block) {
    // Check if shadow sponge is enabled
    if (!SettingsHandler.isSpongeEnabled("shadowSponge")) {
        block.dimension.getBlock(block.location)?.setPermutation(BlockPermutation.resolve("minecraft:air"));
        // Notify nearby players
        const nearbyPlayers = world.getDimension(block.dimension.id).getPlayers({ location: block.location, maxDistance: 10 });
        for (const player of nearbyPlayers) {
            player.sendMessage("§cShadow Sponge is disabled in settings!");
        }
        return;
    }
    const blockId = `${block.location.x}_${block.location.y}_${block.location.z}`;
    if (!shadowSpongeMap.has(blockId)) {
        shadowSpongeMap.set(blockId, Date.now() + (30 * 1000));
    }
    else {
        const time = shadowSpongeMap.get(blockId);
        if (time != undefined && time < Date.now()) {
            shadowSpongeMap.delete(blockId);
            block.dimension.getBlock(block.location)?.setPermutation(BlockPermutation.resolve("minecraft:air"));
            // Remove only particles for this specific block
            for (let i = scheduledParticles.length - 1; i >= 0; i--) {
                if (scheduledParticles[i].blockId === blockId) {
                    scheduledParticles.splice(i, 1);
                }
            }
        }
    }
    const players = world.getDimension(block.dimension.id).getEntities({ maxDistance: 10, type: "minecraft:player", location: block.location });
    if (players.length == 0)
        return;
    let shouldSpawnParticle = false;
    for (const player of players) {
        // Check if player doesn't have invisibility effect
        if (!player.getEffect("minecraft:invisibility")) {
            shouldSpawnParticle = true;
        }
        player.addEffect("minecraft:invisibility", 200, { amplifier: 1, showParticles: false });
    }
    // Only spawn particle if at least one player didn't have invisibility
    if (shouldSpawnParticle) {
        world.getDimension(block.dimension.id).spawnParticle("wildphire_spg:shadow_sponge_particle", { x: block.location.x + 0.5, y: block.location.y, z: block.location.z + 0.5 });
    }
    world.getDimension(block.dimension.id).playSound("block.portal.ambient", block.location);
    // Add ring particles to the loop schedule
    for (let i = 0; i <= 3; i++) {
        scheduledParticles.push({
            location: block.location,
            dimension: block.dimension,
            blockId: blockId
        });
    }
}
system.runInterval(() => {
    if (scheduledParticles.length > 0) {
        const particle = scheduledParticles[0];
        world.getDimension(particle.dimension.id).spawnParticle("wildphire_spg:shadow_ring_sponge_particle", { x: particle.location.x + 0.5, y: particle.location.y, z: particle.location.z + 0.5 });
        scheduledParticles.shift();
    }
}, 60);
