import { world } from "@minecraft/server";
import { SettingsHandler } from "./settings";
let magnetSpongeMap = new Map();
export function magnetSpongePulse(block) {
    // Check if magnet sponge is enabled
    if (!SettingsHandler.isSpongeEnabled("magnetSponge")) {
        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("§cMagnet Sponge is disabled in settings!");
        }
        return;
    }
    const blockId = `${block.location.x}_${block.location.y}_${block.location.z}`;
    if (!magnetSpongeMap.has(blockId)) {
        magnetSpongeMap.set(blockId, Date.now() + (45 * 1000));
    }
    else {
        const time = magnetSpongeMap.get(blockId);
        if (time != undefined && time < Date.now()) {
            magnetSpongeMap.delete(blockId);
            world.getDimension(block.dimension.id).runCommand(`setblock ${block.location.x} ${block.location.y} ${block.location.z} air destroy`);
            return;
        }
    }
    // Get nearby items within a 15 block radius
    const items = world.getDimension(block.dimension.id).getEntities({
        maxDistance: 15,
        type: "item",
        location: block.location
    });
    if (items.length > 0) {
        const targetPos = {
            x: block.location.x + 0.5,
            y: block.location.y + 1,
            z: block.location.z + 0.5
        };
        let itemMoved = false;
        for (const item of items) {
            // Only teleport if the item isn't already at the target position
            const dx = Math.abs(item.location.x - targetPos.x);
            const dy = Math.abs(item.location.y - targetPos.y);
            const dz = Math.abs(item.location.z - targetPos.z);
            if (dx > 0.1 || dy > 0.1 || dz > 0.1) {
                // Teleport the item directly above the sponge
                item.teleport(targetPos);
                itemMoved = true;
            }
        }
        // Play effects only once if any items were moved
        if (itemMoved) {
            world.getDimension(block.dimension.id).playSound("beacon.power", block.location, { pitch: 1.5, volume: 0.5 });
            world.getDimension(block.dimension.id).spawnParticle("wildphire_spg:magnet_ring_particle", {
                x: block.location.x + 0.5,
                y: block.location.y,
                z: block.location.z + 0.5
            });
        }
    }
}
