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

export const LoggerConfig = {
    ENABLE: true,
    CONSOLE_MODE: false,
    SINGLE_OUT: false,
    BLOCKED_TAGS: []
}

let lastText = "";

export function log(tag, ...messages) {
    if(!LoggerConfig.ENABLE) {
        return;
    }

    if(LoggerConfig.BLOCKED_TAGS.includes(tag)) {
        return;
    }

    let secs = Math.floor((system.currentTick+1)/TicksPerSecond) % 60;

    let mins = Math.floor(Math.floor((system.currentTick+1)/TicksPerSecond) / 60);

    if(secs < 10) {
        secs = "0"+secs;
    }

    if(mins < 10) {
        mins = "0"+mins;
    }

    let text = "";

    for(let msg of messages) {
        if(msg instanceof Error) {
            text += "Error(§b"+msg.name+"§r) { " + msg.cause + ": ";

            text += msg.message + "\n";

            text += "   caused by "+msg.stack + " }";
        } else if(msg) {
            text += JSON.stringify(msg);
        } else {
            text += `${msg}`;
        }

        text += " ";
    }

    if(text == lastText && LoggerConfig.SINGLE_OUT) {
        return;
    }

    lastText = text;

    if(LoggerConfig.CONSOLE_MODE) {
        console.error("["+tag+"]", "["+mins+":"+secs+"] ", text);
    } else {
        world.sendMessage("§g["+tag+"]§r§b["+mins+":"+secs+"]§r "+text);
    }
}