import { Player, system, world } from "@minecraft/server"
import { Vector3 } from "../../vector/vector3"
import { colours } from "../../util/index"

/**
 * 
 * @param {Player} player 
 */
export function teleportCompass(player) {
    const dim = player.dimension
    try {
        let spawn = player.getSpawnPoint()
        if (spawn == undefined) {
            spawn = world.getDefaultSpawnLocation()
            spawn = dim.getTopmostBlock(spawn).location
            spawn.y += 1
        }
        player.teleport(new Vector3(spawn.x, spawn.y, spawn.z), { dimension: spawn.dimension})
    }
    catch (e) {
        player.sendMessage(`${colours.dark_red}Error: ${colours.gray}You do not have a spawn point set.`)
        // const spawn = world.getDefaultSpawnLocation()
        // const fixedSpawn = new Vector3(spawn.x, 319, spawn.z)
        // player.teleport(fixedSpawn)
        // system.runTimeout(() => {
        //     while (true) {
        //         world.sendMessage(`Checking ${fixedSpawn}`)
        //         if ( dim.getBlock(fixedSpawn) != undefined && dim.getBlock(fixedSpawn).isAir && dim.getBlock(fixedSpawn).above().isAir && !dim.getBlock(fixedSpawn).below().isAir) {
        //             player.teleport(fixedSpawn, { dimension: dim})
        //             break
        //         }
        //         fixedSpawn.y -= 1
        //     }
        // }, 20)
        
    }
}