import { color, print } from "./graphics";

interface LogEntry {
  frame_age: number;
  line: string;
}

const lines: Map<string, LogEntry> = new Map();

export function log(key: string, ...args: unknown[]) {
  const entry = {
    frame_age: 60,
    line: key + " " + args.join(" "),
  };
  lines.set(key, entry);
}

export function draw_log() {
  color(1, 1, 1, 1);
  let line_y = 3;

  const keys = [...lines.keys()].sort();
  for (const key of keys) {
    const entry = lines.get(key);
    if (entry) {
      // Life is too short for me to convince typescript.
      print(3, line_y, entry.line);
      line_y += 8;
      entry.frame_age -= 1;
      if (entry.frame_age <= 0) {
        lines.delete(key);
      }
    }
  }
}
