import { world, system } from "@minecraft/server";

export class Debug {
    static printMembers(obj) {
        world.sendMessage("Members:");
        if(obj == null) {
            world.sendMessage(`§8[${obj}]`);
            return;
        }
        if(obj instanceof Array) {
            world.sendMessage("§7  [" + obj.length + " elements]");
            for(let key of Object.keys(obj)) {
                let value = obj[key];
                if(typeof(value) === "function") {
                    world.sendMessage(`  - §7${key}: function ${value.name}()`);
                } else {
                    world.sendMessage(`  - §7${key}: ${value}`);
                }
            }
        } else {
            for(let key of Object.keys(obj)) {
                let value = obj[key];
                if(typeof(value) === "function") {
                    world.sendMessage(`  - §7${key}()`);
                } else {
                    world.sendMessage(`  - §7${key}`);
                }
            }
            let prototype = Object.getPrototypeOf(obj);
            while(prototype != null && prototype != Object.prototype && prototype != Array.prototype) {
        
                world.sendMessage("§7  [" + prototype.constructor.name + "]");
                for(let key of Object.getOwnPropertyNames(prototype)) {
                    if(key === "constructor") continue;
                    if(typeof(prototype[key]) === "function") {
                        world.sendMessage("  - §7" + key + "()");
                    } else {
                        world.sendMessage("  - §7" + key);
                    }
                }
        
                prototype = Object.getPrototypeOf(prototype);
            }
        }
    }

    static assert(condition, ...data) {
        if(!condition) {
            if(data.length > 0)
                console.warn(`Assertion failed: ${data.join(' ')}`);
            else
                console.warn(`Assertion failed`);
        }
    }
}