import { system, world } from "@minecraft/server";
import { SettingsHandler } from "./settings";
const explosionSpongeMap = new Map();
function locationKey(loc) {
    return `${loc.x}_${loc.y}_${loc.z}`;
}
function getAreaAround(center, radius) {
    return {
        pos1: {
            x: Math.min(center.x - radius, center.x + radius),
            y: Math.min(center.y - radius, center.y + radius),
            z: Math.min(center.z - radius, center.z + radius)
        },
        pos2: {
            x: Math.max(center.x - radius, center.x + radius),
            y: Math.max(center.y - radius, center.y + radius),
            z: Math.max(center.z - radius, center.z + radius)
        }
    };
}
export function explosionSpongeTick(block) {
    // Check if explosion sponge is enabled
    if (!SettingsHandler.isSpongeEnabled("explosionSponge")) {
        const dimension = block.dimension;
        dimension.runCommand(`setblock ${block.location.x} ${block.location.y} ${block.location.z} air destroy`);
        // Notify nearby players
        const nearbyPlayers = dimension.getPlayers({ location: block.location, maxDistance: 10 });
        for (const player of nearbyPlayers) {
            player.sendMessage("§cExplosion Sponge is disabled in settings!");
        }
        return;
    }
    const blockLoc = block.location;
    const key = locationKey(blockLoc);
    world.getDimension(block.dimension.id).spawnParticle("wildphire_spg:explosion_idle_sponge_particle", {
        x: blockLoc.x + 0.5,
        y: blockLoc.y + 0.5,
        z: blockLoc.z + 0.5
    });
    // Always remove fireballs in the area
    const dimension = block.dimension;
    const entities = dimension.getEntities({ location: blockLoc, maxDistance: 30 });
    for (const entity of entities) {
        if (entity.typeId === "minecraft:fireball" ||
            entity.typeId === "minecraft:wither_skull" ||
            entity.typeId === "minecraft:wither_skull_dangerous") {
            entity.remove();
        }
    }
    // Only register sponge once
    if (!explosionSpongeMap.has(key)) {
        const area = getAreaAround(blockLoc, 30);
        explosionSpongeMap.set(key, area);
    }
}
export function explosionSponge(location, dimension) {
    for (const [key, area] of explosionSpongeMap.entries()) {
        const { pos1, pos2 } = area;
        const within = location.x >= pos1.x && location.x <= pos2.x &&
            location.y >= pos1.y && location.y <= pos2.y &&
            location.z >= pos1.z && location.z <= pos2.z;
        if (within) {
            const [x, y, z] = key.split("_").map(Number);
            const spongeLoc = { x, y, z };
            const block = dimension.getBlock(spongeLoc);
            if (block) {
                system.run(() => {
                    const dim = world.getDimension(dimension.id);
                    dim.playSound("random.pop", spongeLoc);
                    dim.playSound("extinguish.candle", spongeLoc);
                    dim.spawnParticle("minecraft:explosion_manual", location);
                    dim.spawnParticle("wildphire_spg:explosion_sponge_particle", { x: spongeLoc.x + 0.5, y: spongeLoc.y, z: spongeLoc.z + 0.5 });
                });
            }
            return true;
        }
    }
    return false;
}
export function removeExplosionSpongeBlock(location, dimension) {
    const key = locationKey(location);
    if (explosionSpongeMap.has(key)) {
        explosionSpongeMap.delete(key);
        const block = dimension.getBlock(location);
        if (block) {
            system.run(() => {
                world.getDimension(block.dimension.id).runCommand(`setblock ${block.location.x} ${block.location.y} ${block.location.z} air destroy`);
                world.getDimension(dimension.id).playSound("random.pop", location);
            });
        }
        return true;
    }
    return false;
}
