// ===========================
// Animated Counter
// ===========================

document.addEventListener("DOMContentLoaded", () => {

    const counters = document.querySelectorAll(".counter");

    const observer = new IntersectionObserver((entries) => {

        entries.forEach(entry => {

            if (!entry.isIntersecting) return;

            const counter = entry.target;

            const target = parseInt(counter.dataset.target);

            let current = 0;

            const duration = 2000; // 2 seconds

            const increment = target / (duration / 16);

            function updateCounter() {

                current += increment;

                if (current < target) {

                    counter.innerText = Math.floor(current).toLocaleString();

                    requestAnimationFrame(updateCounter);

                } else {

                    counter.innerText = target.toLocaleString();

                }

            }

            updateCounter();

            observer.unobserve(counter);

        });

    }, {
        threshold: 0.5
    });

    counters.forEach(counter => {

        observer.observe(counter);

    });

});


// Reveal Sections

const reveals = document.querySelectorAll('.reveal');

function revealSections(){

    reveals.forEach(section=>{

        const windowHeight = window.innerHeight;

        const revealTop = section.getBoundingClientRect().top;

        const revealPoint = 120;

        if(revealTop < windowHeight - revealPoint){

            section.classList.add('active');

        }

    });

}

window.addEventListener('scroll', revealSections);

window.addEventListener('load', revealSections);