import { ItemStack, system, world } from "@minecraft/server";
import { SettingsHandler } from "./settings";
// Track active silk sponges
const activeSponges = new Map();
// Helper to get a unique key for a location
function getLocationKey(loc) {
    return `${loc.x},${loc.y},${loc.z}`;
}
// Set up one event handler for all silk sponges
world.afterEvents.playerBreakBlock.subscribe((event) => {
    const breakLoc = event.block.location;
    // Check against all active sponges
    for (const spongeLoc of activeSponges.values()) {
        const distance = Math.sqrt(Math.pow(breakLoc.x - spongeLoc.x, 2) +
            Math.pow(breakLoc.y - spongeLoc.y, 2) +
            Math.pow(breakLoc.z - spongeLoc.z, 2));
        if (distance <= 15) { // 15 block radius
            // Store the broken block type for later use
            const brokenBlockId = event.brokenBlockPermutation.type.id;
            // Spawn particle immediately at break location
            event.dimension.spawnParticle("wildphire_spg:silk_sponge_particle", {
                x: breakLoc.x + 0.5,
                y: breakLoc.y + 0.5,
                z: breakLoc.z + 0.5
            });
            // Use a very short delay to ensure the item has spawned
            system.runTimeout(() => {
                // Get items in a small area around the broken block
                const items = event.dimension.getEntities({
                    location: {
                        x: breakLoc.x + 0.5,
                        y: breakLoc.y + 0.5,
                        z: breakLoc.z + 0.5
                    },
                    maxDistance: 1,
                    type: "item"
                });
                // Remove items at the block's position
                for (const item of items) {
                    item.remove();
                }
                // Spawn the silk touch version
                const itemStack = new ItemStack(brokenBlockId, 1);
                event.dimension.spawnItem(itemStack, {
                    x: breakLoc.x + 0.5,
                    y: breakLoc.y,
                    z: breakLoc.z + 0.5
                });
            }, 1); // 1 tick delay
            break; // No need to check other sponges
        }
    }
});
export function silkSpongePulse(block) {
    // Check if silk sponge is enabled
    if (!SettingsHandler.isSpongeEnabled("silkSponge")) {
        const dim = block.dimension;
        dim.runCommand(`setblock ${block.location.x} ${block.location.y} ${block.location.z} air destroy`);
        // Notify nearby players
        const nearbyPlayers = dim.getPlayers({ location: block.location, maxDistance: 10 });
        for (const player of nearbyPlayers) {
            player.sendMessage("§cSilk Sponge is disabled in settings!");
        }
        return;
    }
    const key = getLocationKey(block.location);
    // Add this sponge to active sponges if it's not already there
    if (!activeSponges.has(key)) {
        activeSponges.set(key, block.location);
        // Set up self-destruct timer (30 seconds)
        system.runTimeout(() => {
            // Remove from active sponges
            activeSponges.delete(key);
            // Set block to air
            if (block.dimension.getBlock(block.location)?.typeId === "wildphire_spg:silk_sponge") {
                block.dimension.getBlock(block.location)?.setType("minecraft:air");
            }
        }, 600); // 30 seconds = 20 ticks/second * 30 = 600 ticks
    }
}
// Export a function to remove sponges when they're destroyed
export function removeSilkSponge(location) {
    const key = getLocationKey(location);
    activeSponges.delete(key);
}
