// styles.el -- Landing page JS: nav scroll, scroll-reveal, founding counter polling,
//              checkout button routing.
// Compiled with: elc --target=js --bundle --minify --obfuscate

fn main() -> Void {
    native_js("(function() {
  'use strict';

  // Nav scroll effect
  var nav = document.getElementById('nav');
  if (nav) {
    window.addEventListener('scroll', function() {
      if (window.scrollY > 40) {
        nav.classList.add('scrolled');
      } else {
        nav.classList.remove('scrolled');
      }
    }, { passive: true });
  }

  // Auto-open chat if ?open=chat in URL
  if (typeof URLSearchParams !== 'undefined' && new URLSearchParams(window.location.search).get('open') === 'chat') {
    setTimeout(function() { if (typeof neuronDemoToggle === 'function') neuronDemoToggle(); }, 600);
  }

  // Scroll reveal via IntersectionObserver
  var revealEls = document.querySelectorAll('.reveal');
  if ('IntersectionObserver' in window) {
    var observer = new IntersectionObserver(function(entries) {
      entries.forEach(function(entry) {
        if (entry.isIntersecting) {
          entry.target.classList.add('visible');
          observer.unobserve(entry.target);
        }
      });
    }, { threshold: 0.12, rootMargin: '0px 0px -40px 0px' });
    revealEls.forEach(function(el) { observer.observe(el); });
  } else {
    revealEls.forEach(function(el) { el.classList.add('visible'); });
  }

  // Founding counter polling
  var prevSold = null;
  function updateFoundingUI(data) {
    var remaining = data.remaining;
    var sold      = data.sold;
    var total     = data.total;
    var pct       = Math.round((sold / total) * 100);
    var flash     = prevSold !== null && sold > prevSold;
    prevSold = sold;

    var spotLabel = document.querySelector('.founding-spots-label');
    if (spotLabel) spotLabel.textContent = 'Only ' + remaining + ' left';
    var spotFill  = document.querySelector('.founding-spots-fill');
    if (spotFill)  spotFill.style.width  = pct + '%';
    var spotSub   = document.querySelector('.founding-spots-sub');
    if (spotSub)   spotSub.textContent   = sold + ' of ' + total + ' claimed';

    var bannerCount = document.querySelector('.founding-banner-count');
    if (bannerCount) {
      bannerCount.textContent = remaining;
      if (flash) {
        bannerCount.style.color = '#0078D4';
        setTimeout(function() { bannerCount.style.color = ''; }, 1200);
      }
    }
    var bannerFill = document.querySelector('.founding-banner-fill');
    if (bannerFill) bannerFill.style.width = pct + '%';
  }

  function pollFoundingCount() {
    fetch('/api/founding-count')
      .then(function(r) { return r.json(); })
      .then(function(data) { updateFoundingUI(data); })
      .catch(function() {});
  }
  pollFoundingCount();
  setInterval(pollFoundingCount, 90000);

  // Hide chat widget on non-marketing pages
  if (window.location.pathname.indexOf('/checkout') === 0 ||
      window.location.pathname.indexOf('/account') === 0 ||
      window.location.pathname.indexOf('/legal') === 0 ||
      window.location.pathname.indexOf('/marketplace/success') === 0) {
    var demoBtn   = document.getElementById('neuron-demo-btn');
    var demoPanel = document.getElementById('neuron-demo-panel');
    if (demoBtn)   demoBtn.style.display   = 'none';
    if (demoPanel) demoPanel.style.display = 'none';
  }

  // Checkout buttons
  var checkoutBtns = document.querySelectorAll('[data-checkout]');
  checkoutBtns.forEach(function(btn) {
    btn.addEventListener('click', function() {
      window.location.href = '/checkout?plan=' + btn.getAttribute('data-checkout');
    });
  });
})()")
}
