class CounterAnimation {
  constructor() {
    document.querySelectorAll('.counter').forEach(el => {
      new IntersectionObserver(([e]) => {
        if (!e.isIntersecting) return;
        const t = parseFloat(el.dataset.target), d = parseInt(el.dataset.decimals), dur = 1800, s = performance.now();
        const ease = x => x < 0.5 ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2;
        const u = n => {
          const p = Math.min((n - s) / dur, 1);
          el.textContent = (t * ease(p)).toFixed(d);
          if (p < 1) requestAnimationFrame(u);
        };
        u(s);
        e.target._counted = true;
      }, { threshold: 0.5 }).observe(el);
    });
  }
}
