import { world, BlockPermutation } from "@minecraft/server";
import { SettingsHandler } from "./settings";
let thornSpongeMap = new Map();
export function thornSpongePulse(block) {
    // Check if thorn sponge is enabled
    if (!SettingsHandler.isSpongeEnabled("thornSponge")) {
        world.getDimension(block.dimension.id).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("§cThorn Sponge is disabled in settings!");
        }
        return;
    }
    const blockId = `${block.location.x}_${block.location.y}_${block.location.z}`;
    if (!thornSpongeMap.has(blockId)) {
        thornSpongeMap.set(blockId, Date.now() + (30 * 1000)); // 30 seconds
    }
    else {
        const time = thornSpongeMap.get(blockId);
        if (time != undefined && time < Date.now()) {
            thornSpongeMap.delete(blockId);
            world.getDimension(block.dimension.id).getBlock(block.location)?.setPermutation(BlockPermutation.resolve("minecraft:air"));
            return;
        }
    }
    // Damage mobs (not players) in a 15 block radius
    const entities = world.getDimension(block.dimension.id).getEntities({ maxDistance: 10, location: block.location });
    if (entities.length > 0) {
        world.getDimension(block.dimension.id).spawnParticle("wildphire_spg:thorn_block_sponge_particle", {
            x: block.location.x + 0.5,
            y: block.location.y,
            z: block.location.z + 0.5
        });
    }
    for (const entity of entities) {
        if (entity.typeId !== "minecraft:player" && entity.typeId !== "minecraft:item" && entity.typeId !== "minecraft:armor_stand") {
            try {
                entity.applyDamage(2);
                world.getDimension(block.dimension.id).spawnParticle("wildphire_spg:thorn_sponge_particle", {
                    x: entity.location.x + 0.5,
                    y: entity.location.y,
                    z: entity.location.z + 0.5
                });
            }
            catch (e) {
                // Skip entities that can't be damaged
                continue;
            }
        }
    }
}
