// The War page — live war cost counter.
//
// Pulls from public 2023-2024 baselines (SIPRI, US DoD) and computes a
// per-second tick so the numbers actually move on screen. Total US
// military budget, global military spending, US arms exports, Tomahawk
// missile cost, F-35 operating cost — translated into the lived currency
// (a school built, a year of housing, a teacher's salary).

(function () {
  const root = document.getElementById("warclock");
  if (!root) return;
  const view = document.querySelector('.view[data-view="war"]');
  if (!view) return;

  const t = (k, fb) => (window.i18n && window.i18n.t) ? window.i18n.t(k, fb) : fb;

  // Annual baselines (2024). Per-second derived inline.
  const US_MIL_ANNUAL      = 916_000_000_000;    // $916B
  const GLOBAL_MIL_ANNUAL  = 2_443_000_000_000;  // $2.44T (SIPRI 2023)
  const US_ARMS_ANNUAL     = 80_900_000_000;     // record FY2023 exports
  const F35_HOURLY         = 36_000;             // per flight hour
  const TOMAHAWK_COST      = 2_000_000;          // per missile
  const TOMAHAWK_PER_YEAR  = 80;                 // illustrative steady-state
  // Conservative civilian-conflict-deaths estimate (ACLED 2023 ~ 170k+);
  // we use 100k/yr to stay conservative against headline numbers.
  const CIV_DEATHS_ANNUAL  = 100_000;

  const SECONDS_PER_YEAR   = 31_557_600;
  const US_PER_SEC         = US_MIL_ANNUAL     / SECONDS_PER_YEAR;
  const GLOBAL_PER_SEC     = GLOBAL_MIL_ANNUAL / SECONDS_PER_YEAR;
  const ARMS_PER_SEC       = US_ARMS_ANNUAL    / SECONDS_PER_YEAR;
  const F35_PER_SEC        = F35_HOURLY        / 3600;
  const TOMAHAWK_PER_SEC   = TOMAHAWK_COST * TOMAHAWK_PER_YEAR / SECONDS_PER_YEAR;
  const CIV_PER_SEC        = CIV_DEATHS_ANNUAL / SECONDS_PER_YEAR;

  const T_OPEN = Date.now();

  const usEl       = root.querySelector(".wc-us");
  const globalEl   = root.querySelector(".wc-global");
  const armsEl     = root.querySelector(".wc-arms");
  const f35El      = root.querySelector(".wc-f35");
  const tomahawkEl = root.querySelector(".wc-tomahawk");
  const civEl      = root.querySelector(".wc-civ");
  const timeEl     = root.querySelector(".wc-time");

  function fmt$(v) {
    return "$" + Math.floor(v).toLocaleString("en-US");
  }
  function fmtInt(v) {
    return Math.floor(v).toLocaleString("en-US");
  }
  function fmtSec(s) {
    const m = Math.floor(s / 60);
    const sec = Math.floor(s % 60);
    return m + ":" + (sec < 10 ? "0" : "") + sec;
  }

  let rafId = null;
  function tick() {
    const dt = (Date.now() - T_OPEN) / 1000;
    usEl.textContent       = fmt$(US_PER_SEC       * dt);
    globalEl.textContent   = fmt$(GLOBAL_PER_SEC   * dt);
    armsEl.textContent     = fmt$(ARMS_PER_SEC     * dt);
    f35El.textContent      = fmt$(F35_PER_SEC      * dt);
    tomahawkEl.textContent = fmt$(TOMAHAWK_PER_SEC * dt);
    civEl.textContent      = fmtInt(CIV_PER_SEC    * dt);
    timeEl.textContent     = fmtSec(dt);
    rafId = requestAnimationFrame(tick);
  }
  function isActive() { return view.classList.contains("active"); }
  function start() { if (rafId !== null) return; tick(); }
  function stop() { if (rafId === null) return; cancelAnimationFrame(rafId); rafId = null; }

  if ("MutationObserver" in window) {
    new MutationObserver(() => { isActive() ? start() : stop(); })
      .observe(view, { attributes: true, attributeFilter: ["class"] });
    if (isActive()) start();
  } else { start(); }
  document.addEventListener("visibilitychange", () => {
    if (document.hidden) stop();
    else if (isActive()) start();
  });
})();
