// The Money page — live fiat decay counter.
//
// Anchored to August 15, 1971, the day Nixon ended dollar-gold convertibility
// (the "Nixon Shock"). From that day forward every fiat currency floats free
// of any commodity anchor, and the unit of account silently shrinks.
//
// US CPI 1971 = 40.5, current ≈ 314 (BLS), giving ~7.76× cumulative inflation
// — a geometric annual rate of ~3.95%. We compute purchasing power on every
// frame so the last digits actually move. The Fed M2 money supply has grown
// from ~$685B in 1971 to ~$21T today, ~30×, so we also tick a "printed since
// you opened this page" counter at the average post-1971 M2 expansion rate.

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

  // Anchor: Nixon Shock — Aug 15, 1971, 21:00 ET.
  const T0_MS    = Date.UTC(1971, 7, 16, 1, 0, 0); // ~Aug 16 01:00 UTC
  // BLS-derived: cumulative 1971 → present.
  const CUM_INFLATION = 7.76;          // $1 in 1971 = ~$7.76 today
  const YEARS_ELAPSED = (Date.now() - T0_MS) / (365.25 * 24 * 3600 * 1000);
  const ANNUAL_RATE   = Math.pow(CUM_INFLATION, 1 / YEARS_ELAPSED) - 1; // ~0.0395
  // Federal Reserve M2 ~ $21T, growing ~6.5% / yr long-run average post-1971.
  const M2_TODAY     = 21_300_000_000_000; // dollars
  const M2_GROWTH    = 0.065;
  const M2_PER_SEC   = M2_TODAY * (Math.pow(1 + M2_GROWTH, 1 / 31_557_600) - 1);

  const pwEl   = root.querySelector(".mc-pw");
  const lossEl = root.querySelector(".mc-loss");
  const printEl= root.querySelector(".mc-print");
  const timeEl = root.querySelector(".mc-time");

  function purchasingPowerNow() {
    const years = (Date.now() - T0_MS) / (365.25 * 24 * 3600 * 1000);
    return 1 / Math.pow(1 + ANNUAL_RATE, years);
  }
  const PW_AT_OPEN = purchasingPowerNow();
  const T_OPEN     = Date.now();

  function fmtUSD(v, decimals) {
    return "$" + v.toLocaleString("en-US", {
      minimumFractionDigits: decimals,
      maximumFractionDigits: decimals
    });
  }
  function fmtInt(v) {
    return Math.floor(v).toLocaleString("en-US");
  }
  function fmtSeconds(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 now = Date.now();
    const pwNow = purchasingPowerNow();
    pwEl.textContent = fmtUSD(pwNow, 8);
    const lost = (PW_AT_OPEN - pwNow);
    lossEl.textContent = fmtUSD(lost, 10);
    const dtSec = (now - T_OPEN) / 1000;
    const printed = M2_PER_SEC * dtSec;
    printEl.textContent = fmtInt(printed);
    timeEl.textContent = fmtSeconds(dtSec);
    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();
  });
})();
