import { MolangVariableMap, BlockPermutation } from "@minecraft/server";
import { SettingsHandler } from "./settings";
const DURATION = 15 * 1000; // 15 seconds in milliseconds
const activeSponges = new Map();
export function smokeSpongePlace(block) {
    // Check if smoke sponge is enabled
    if (!SettingsHandler.isSpongeEnabled("smokeSponge")) {
        const currentBlock = block.dimension.getBlock(block.location);
        if (currentBlock?.typeId === "wildphire_spg:smoke_sponge") {
            const newState = BlockPermutation.resolve("minecraft:air");
            currentBlock.setPermutation(newState);
        }
        // Notify nearby players
        const nearbyPlayers = block.dimension.getPlayers({ location: block.location, maxDistance: 10 });
        for (const player of nearbyPlayers) {
            player.sendMessage("§cSmoke Sponge is disabled in settings!");
        }
        return;
    }
    const location = `${block.location.x},${block.location.y},${block.location.z}`;
    if (!activeSponges.has(location)) {
        activeSponges.set(location, Date.now() + DURATION);
    }
    smokeSpongePulse(block);
}
export function smokeSpongePulse(block) {
    // Check if smoke sponge is enabled
    if (!SettingsHandler.isSpongeEnabled("smokeSponge")) {
        const currentBlock = block.dimension.getBlock(block.location);
        if (currentBlock?.typeId === "wildphire_spg:smoke_sponge") {
            const newState = BlockPermutation.resolve("minecraft:air");
            currentBlock.setPermutation(newState);
        }
        // Notify nearby players
        const nearbyPlayers = block.dimension.getPlayers({ location: block.location, maxDistance: 10 });
        for (const player of nearbyPlayers) {
            player.sendMessage("§cSmoke Sponge is disabled in settings!");
        }
        return;
    }
    const location = `${block.location.x},${block.location.y},${block.location.z}`;
    // If sponge isn't in the map yet, add it
    if (!activeSponges.has(location)) {
        activeSponges.set(location, Date.now() + DURATION);
    }
    const expiryTime = activeSponges.get(location);
    const currentTime = Date.now();
    if (currentTime >= expiryTime) {
        activeSponges.delete(location);
        const currentBlock = block.dimension.getBlock(block.location);
        if (currentBlock?.typeId === "wildphire_spg:smoke_sponge") {
            const newState = BlockPermutation.resolve("minecraft:air");
            currentBlock.setPermutation(newState);
        }
        return;
    }
    // Apply darkness to nearby players
    const nearbyPlayers = block.dimension.getPlayers({
        location: block.location,
        maxDistance: 15
    });
    for (const player of nearbyPlayers) {
        player.addEffect("darkness", 100, { amplifier: 3, showParticles: false });
    }
    // Spawn custom smoke particles on sponge corners and players
    const molang = new MolangVariableMap();
    // Spawn particles 4 blocks away on the diagonals
    const corners = [
        { x: -4, y: 0, z: -4 },
        { x: -4, y: 0, z: 4 },
        { x: 4, y: 0, z: -4 },
        { x: 4, y: 0, z: 4 },
        { x: 0, y: 0, z: 0 }
    ];
    // Spawn particles at each corner
    for (const offset of corners) {
        const cornerPos = {
            x: block.location.x + offset.x,
            y: block.location.y + offset.y,
            z: block.location.z + offset.z
        };
        block.dimension.spawnParticle("wildphire_spg:smoke_sponge_particle", cornerPos, molang);
    }
}
