import { registerLoadCallback } from "./main.js";
import { awaitEvent, emitEvent, listenTo } from "./events.js";
import { loadExampleFile } from "./files.js";
import { loadConfiguration } from "./configuration.js";
import { recall, save } from "./storage.js";
import { getElements } from "./dom-utils.js";

const devModeKey = "dev-mode";
let devMode = false;

registerLoadCallback(() => {
  devMode = recall(devModeKey, false);

  getElements("toggle", "dev-mode:toggle").forEach(e => {
    e.dataset.toggled = devMode.toString();
  });

  listenTo("dev-mode:toggle", (e) => {
    devMode = e.detail.state;
    save(devModeKey, devMode);
    console.log("debug mode toggled to", devMode);
  });

  document.addEventListener("keydown", e => {
    if (!devMode) return;

    if (e.key === "H") {
      emitEvent("dev:suppress-hover");
    }
    if (e.key === "T") {
      emitEvent("dev:suppress-tooltips");
    }
  });

  if (!devMode) return;

  awaitEvent("setup:end").then(() => {
    void loadConfiguration("default");
  });

  awaitEvent("config:end").then(() => {
    void loadExampleFile("Pandora's Fission Reactor");
  });

  queueMicrotask(() => emitEvent("dev-mode:end"));
});
