/*
  Telent Space Strategy — page behaviour.
  No external dependencies beyond the vendored bootstrap.bundle.min.js (nav collapse only).
*/
(function () {
  "use strict";

  /* ---------- Hero starfield ---------- */
  function initStarfield() {
    var canvas = document.getElementById("starfield");
    if (!canvas) return;
    var ctx = canvas.getContext("2d");
    var stars = [];
    var reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;

    function resize() {
      canvas.width = canvas.offsetWidth * window.devicePixelRatio;
      canvas.height = canvas.offsetHeight * window.devicePixelRatio;
    }

    function seed() {
      var count = Math.floor((canvas.width * canvas.height) / 9000);
      stars = [];
      for (var i = 0; i < count; i++) {
        stars.push({
          x: Math.random() * canvas.width,
          y: Math.random() * canvas.height,
          r: Math.random() * 1.4 + 0.3,
          phase: Math.random() * Math.PI * 2,
          speed: Math.random() * 0.015 + 0.005
        });
      }
    }

    function draw(t) {
      ctx.clearRect(0, 0, canvas.width, canvas.height);
      ctx.fillStyle = "#ffffff";
      for (var i = 0; i < stars.length; i++) {
        var s = stars[i];
        var twinkle = reduceMotion ? 0.8 : 0.5 + 0.5 * Math.sin(s.phase + t * s.speed);
        ctx.globalAlpha = 0.25 + twinkle * 0.6;
        ctx.beginPath();
        ctx.arc(s.x, s.y, s.r, 0, Math.PI * 2);
        ctx.fill();
      }
      ctx.globalAlpha = 1;
      if (!reduceMotion) requestAnimationFrame(draw);
    }

    resize();
    seed();
    window.addEventListener("resize", function () {
      resize();
      seed();
    });
    requestAnimationFrame(draw);
  }

  /* ---------- Scroll-spy nav highlighting ---------- */
  function initScrollSpy() {
    var navLinks = Array.prototype.slice.call(document.querySelectorAll(".navbar-nav .nav-link"));
    if (!navLinks.length) return;
    var sections = navLinks
      .map(function (link) {
        var id = link.getAttribute("href");
        return id && id.charAt(0) === "#" ? document.querySelector(id) : null;
      })
      .filter(Boolean);

    if (!("IntersectionObserver" in window)) return;

    var observer = new IntersectionObserver(
      function (entries) {
        entries.forEach(function (entry) {
          if (!entry.isIntersecting) return;
          var id = "#" + entry.target.id;
          navLinks.forEach(function (link) {
            link.classList.toggle("active", link.getAttribute("href") === id);
          });
        });
      },
      { rootMargin: "-40% 0px -55% 0px", threshold: 0 }
    );

    sections.forEach(function (section) {
      observer.observe(section);
    });

    // collapse the mobile nav after a link is tapped
    navLinks.forEach(function (link) {
      link.addEventListener("click", function () {
        var toggler = document.querySelector(".navbar-toggler");
        var target = document.querySelector(".navbar-collapse.show");
        if (target && toggler && window.bootstrap) {
          window.bootstrap.Collapse.getOrCreateInstance(target).hide();
        }
      });
    });
  }

  /* ---------- Count-up stat numbers ---------- */
  function initCounters() {
    var counters = document.querySelectorAll("[data-count-to]");
    if (!counters.length || !("IntersectionObserver" in window)) return;

    function animate(el) {
      var target = parseFloat(el.getAttribute("data-count-to"));
      var prefix = el.getAttribute("data-prefix") || "";
      var suffix = el.getAttribute("data-suffix") || "";
      var decimals = el.getAttribute("data-decimals") ? parseInt(el.getAttribute("data-decimals"), 10) : 0;
      var duration = 1400;
      var start = null;

      function step(ts) {
        if (start === null) start = ts;
        var progress = Math.min((ts - start) / duration, 1);
        var eased = 1 - Math.pow(1 - progress, 3);
        var value = target * eased;
        el.textContent = prefix + value.toFixed(decimals) + suffix;
        if (progress < 1) requestAnimationFrame(step);
      }
      requestAnimationFrame(step);
    }

    var observer = new IntersectionObserver(
      function (entries, obs) {
        entries.forEach(function (entry) {
          if (entry.isIntersecting) {
            animate(entry.target);
            obs.unobserve(entry.target);
          }
        });
      },
      { threshold: 0.4 }
    );

    counters.forEach(function (el) {
      observer.observe(el);
    });
  }

  /* ---------- Key Info PDF button(s) ---------- */
  function initPrintButton() {
    var btns = document.querySelectorAll(".js-print-key-info");
    btns.forEach(function (btn) {
      btn.addEventListener("click", function () {
        window.print();
      });
    });
  }

  document.addEventListener("DOMContentLoaded", function () {
    initStarfield();
    initScrollSpy();
    initCounters();
    initPrintButton();
  });
})();
