let logLines: string[] = [];
let logLength = 100;
let logVisible = false;

export function logID(ID: string) {
  if (!ID) {
    return "badID";
  }
  
  return ID.substring(0, 5);
}

export function setLogVisibility(visible:boolean) {
    logVisible = visible;
}

export function renderLog() {
  if (!logVisible) {
    return;
  }

  let log = document.getElementById("log");
  if (!log) {
    throw new Error();
  }
  log.innerText = logLines.join("\n");
}

export function log(...args: any[]): any {
  // console.log(...args);

  let logLine = `[${new Date().toLocaleTimeString()}]: `;
  for (let arg of args) {
    let completeLine = (typeof arg === "string" || arg instanceof String) ? arg : JSON.stringify(arg, null, 4);
    if (completeLine === undefined) {
      completeLine = "undefined";
    }
    logLine += completeLine.substring(0, 500);
  }
  logLines.push(logLine + "\n");

  if (logLines.length > logLength) {
    logLines = logLines.slice(logLines.length - logLength);
  }

  renderLog();

  return [logLine];// [...args];
}
