import { system, world } from "@minecraft/server";
import { SettingsHandler } from "./settings";
export function teleportSponge(block) {
    try {
        // Check if teleport sponge is enabled
        if (!SettingsHandler.isSpongeEnabled("teleportSponge")) {
            const dim = world.getDimension(block.dimension.id);
            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("§cTeleport Sponge is disabled in settings!");
            }
            return;
        }
        const dim = world.getDimension(block.dimension.id);
        const spongePos = block.location;
        const spongeY = spongePos.y;
        const players = dim.getEntities({
            type: "minecraft:player",
            location: spongePos,
            maxDistance: 15,
        });
        if (players.length === 0)
            return;
        dim.spawnParticle("wildphire_spg:teleport_sponge_particle", {
            x: spongePos.x + 0.5,
            y: spongePos.y,
            z: spongePos.z + 0.5,
        });
        system.runTimeout(() => {
            dim.spawnParticle("wildphire_spg:teleport_sponge_particle", {
                x: spongePos.x + 0.5,
                y: spongePos.y,
                z: spongePos.z + 0.5,
            });
            let anyTeleported = false;
            for (const player of players) {
                let teleported = false;
                const tryTeleport = (yRange) => {
                    for (let attempt = 0; attempt < 20; attempt++) {
                        const angle = Math.random() * 2 * Math.PI;
                        const distance = Math.random() * 35;
                        const tx = Math.floor(spongePos.x + Math.cos(angle) * distance);
                        const tz = Math.floor(spongePos.z + Math.sin(angle) * distance);
                        const dx = tx - spongePos.x;
                        const dz = tz - spongePos.z;
                        if ((dx * dx + dz * dz) < 64)
                            continue; // Avoid too close
                        const ty = Math.floor(Math.random() * (yRange.max - yRange.min + 1)) + yRange.min;
                        const below = dim.getBlock({ x: tx, y: ty - 1, z: tz });
                        const feet = dim.getBlock({ x: tx, y: ty, z: tz });
                        const head = dim.getBlock({ x: tx, y: ty + 1, z: tz });
                        if (below?.typeId !== "minecraft:air" &&
                            feet?.typeId === "minecraft:air" &&
                            head?.typeId === "minecraft:air") {
                            player.teleport({ x: tx + 0.5, y: ty, z: tz + 0.5 });
                            player.sendMessage(`§8[§5Teleport Sponge§8] §7> §eYou have been teleported!`);
                            system.runTimeout(() => {
                                dim.playSound("mob.shulker.teleport", player.location);
                            }, 1);
                            return true;
                        }
                    }
                    return false;
                };
                // Prioritize Y below sponge
                teleported = tryTeleport({ min: -50, max: spongeY - 1 });
                // If none found, try full Y range
                if (!teleported) {
                    teleported = tryTeleport({ min: -50, max: 320 });
                }
                if (teleported) {
                    anyTeleported = true;
                }
                else {
                    player.onScreenDisplay.setActionBar(`     §8[§5Teleport Sponge§8]\n§eFinding Random Location...`);
                }
            }
            if (anyTeleported) {
                dim.runCommand(`setblock ${spongePos.x} ${spongePos.y} ${spongePos.z} air destroy`);
            }
        }, 60); // 3 seconds
    }
    catch (e) {
    }
}
