import { BlockPermutation, world, EffectTypes, system } from "@minecraft/server";
import { SettingsHandler } from "./settings";
export function jumpSpongePlace(player, block) {
    // Check if jump sponge is enabled
    if (!SettingsHandler.isSpongeEnabled("jumpSponge")) {
        const dimension = world.getDimension(block.dimension.id);
        dimension.runCommand(`setblock ${block.location.x} ${block.location.y} ${block.location.z} air destroy`);
        player.sendMessage("§cJump Sponge is disabled in settings!");
        return;
    }
    const position = block.location;
    const dimension = world.getDimension(block.dimension.id);
    // Get all entities within 3 blocks of the sponge
    const nearbyEntities = dimension.getEntities({
        location: { x: position.x + 0.5, y: position.y + 0.5, z: position.z + 0.5 },
        maxDistance: 3
    });
    let entitiesLaunched = false;
    for (const entity of nearbyEntities) {
        // Launch the entity up to 20 blocks high
        entity.applyKnockback(0, 0, 0, 2.0); // 2.0 ≈ 20 blocks height
        // Give slow falling effect to prevent fall damage (works on players and some mobs)
        const slowFallingEffect = EffectTypes.get("slow_falling");
        if (slowFallingEffect) {
            try {
                entity.addEffect(slowFallingEffect, 200, { amplifier: 0, showParticles: true });
            }
            catch (error) {
                // Some entities might not support effects, continue anyway
            }
        }
        entitiesLaunched = true;
    }
    // Only trigger effects and cooldown if entities were actually launched
    if (entitiesLaunched) {
        // Visual and audio effects
        dimension.spawnParticle("minecraft:wind_explosion_emitter", { x: position.x + 0.5, y: position.y + 1, z: position.z + 0.5 });
        dimension.spawnParticle("wildphire_spg:jump_ring_sponge_particle", { x: position.x + 0.5, y: position.y + 0.5, z: position.z + 0.5 });
        dimension.playSound("firework.launch", position);
        dimension.playSound("entity.player.attack.sweep", position);
        // Add a cooldown by temporarily changing the block
        block.setPermutation(BlockPermutation.resolve("wildphire_spg:jump_sponge_used"));
        // Reset the sponge after 3 seconds
        system.runTimeout(() => {
            const currentBlock = dimension.getBlock(position);
            if (currentBlock && currentBlock.typeId === "wildphire_spg:jump_sponge") {
                currentBlock.setPermutation(BlockPermutation.resolve("minecraft:air"));
            }
        }, 60); // 3 seconds (20 ticks per second)
    }
}
export function jumpSpongePulse(block) {
    // Check if jump sponge is enabled
    if (!SettingsHandler.isSpongeEnabled("jumpSponge")) {
        const dimension = world.getDimension(block.dimension.id);
        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("§cJump Sponge is disabled in settings!");
        }
        return;
    }
    const position = block.location;
    const dimension = world.getDimension(block.dimension.id);
    dimension.spawnParticle("wildphire_spg:jump_ring_sponge_particle", { x: position.x + 0.5, y: position.y, z: position.z + 0.5 });
    // Get all entities within 3 blocks of the sponge
    const nearbyEntities = dimension.getEntities({
        location: { x: position.x + 0.5, y: position.y + 0.5, z: position.z + 0.5 },
        maxDistance: 3
    });
    let entitiesLaunched = false;
    for (const entity of nearbyEntities) {
        try {
            entity.applyKnockback(0, 0, 0, 3.0);
            const slowFallingEffect = EffectTypes.get("slow_falling");
            if (slowFallingEffect) {
                try {
                    entity.addEffect(slowFallingEffect, 200, { amplifier: 0, showParticles: true });
                }
                catch (error) {
                    // Some entities might not support effects, continue anyway
                }
            }
            entitiesLaunched = true;
        }
        catch (error) {
            // Some entities might not support knockback, skip them
        }
    }
    if (entitiesLaunched) {
        dimension.spawnParticle("minecraft:wind_explosion_emitter", { x: position.x + 0.5, y: position.y + 1, z: position.z + 0.5 });
        dimension.playSound("firework.launch", position);
        dimension.playSound("entity.player.attack.sweep", position);
    }
}
