
(function () {
  'use strict';


  function onVisible(el, cb) {
    if (!el) return;
    if (!window.IntersectionObserver) { cb(el); return; }
    var io = new IntersectionObserver(function (entries, obs) {
      entries.forEach(function (e) {
        if (e.isIntersecting) { cb(e.target); obs.unobserve(e.target); }
      });
    }, { threshold: 0.12 });
    io.observe(el);
  }


  function initRv() {
    if (!window.IntersectionObserver) {
      document.querySelectorAll('.rv').forEach(function (el) { el.classList.add('on'); });
      return;
    }
    var io = new IntersectionObserver(function (entries) {
      entries.forEach(function (e) {
        if (e.isIntersecting) { e.target.classList.add('on'); io.unobserve(e.target); }
      });
    }, { threshold: 0.12 });
    document.querySelectorAll('.rv:not(.on)').forEach(function (el) { io.observe(el); });
  }


  function initReveal() {
    if (!window.IntersectionObserver) {
      document.querySelectorAll('.reveal').forEach(function (el) { el.classList.add('visible'); });
      return;
    }
    var io = new IntersectionObserver(function (entries) {
      entries.forEach(function (e) {
        if (e.isIntersecting) { e.target.classList.add('visible'); io.unobserve(e.target); }
      });
    }, { threshold: 0.10 });
    document.querySelectorAll('.reveal:not(.visible)').forEach(function (el) { io.observe(el); });
  }

  function initSkillBars() {
    document.querySelectorAll('.vista-skills-wrap').forEach(function (wrap) {
      onVisible(wrap, function () {
        wrap.querySelectorAll('.sk-item').forEach(function (item) {
          var pct  = parseInt(item.getAttribute('data-pct') || '0');
          var fill = item.querySelector('.sk-fill');
          if (!fill) return;
          fill.style.transition = 'none';
          fill.style.width = '0';
          item.classList.add('on');
          requestAnimationFrame(function () {
            requestAnimationFrame(function () {
              fill.style.transition = 'width 1.8s cubic-bezier(.22,1,.36,1)';
              fill.style.width = pct + '%';
            });
          });
        });
      });
    });
  }

  function animateCounter(countEl, target, duration) {
    var startTime = null;
    function ease(t) { return 1 - Math.pow(1 - t, 3); }
    function step(ts) {
      if (!startTime) startTime = ts;
      var p = Math.min((ts - startTime) / duration, 1);
      countEl.textContent = Math.floor(ease(p) * target);
      if (p < 1) requestAnimationFrame(step);
      else countEl.textContent = target;
    }
    requestAnimationFrame(step);
  }

  function initCounters() {
    document.querySelectorAll('.vista-stats-wrap').forEach(function (wrap) {
      var speed = parseInt(wrap.getAttribute('data-counter-speed')) || 2000;
      var done  = false;
      onVisible(wrap, function () {
        if (done) return;
        done = true;
        wrap.querySelectorAll('.bento-n[data-target]').forEach(function (el) {
          var target   = parseInt(el.getAttribute('data-target'));
          var countEl  = el.querySelector('.bento-count');
          if (!isNaN(target) && countEl) animateCounter(countEl, target, speed);
        });
      });
    });
  }

function initTestimonials() {
    document.querySelectorAll('.vista-testimonials-wrap').forEach(function (wrap) {
      if (wrap._vistaInit) return;
      wrap._vistaInit = true;

      var swiperEl = wrap.querySelector('.swiper');
      var doAuto   = wrap.getAttribute('data-autoplay') === 'true';
      var spd      = parseInt(wrap.getAttribute('data-speed')) || 4000;

      if (!swiperEl || typeof window.Swiper === 'undefined') return;

      var dotsEl = wrap.querySelector('.tctl-dots');
      var prevEl = wrap.querySelector('.swiper-button-prev');
      var nextEl = wrap.querySelector('.swiper-button-next');

      new window.Swiper(swiperEl, {
        loop: false,             
        centeredSlides: true,    
        initialSlide: 1,         
        slidesPerView: "auto",   
        spaceBetween: 24,
        speed: 600,
        grabCursor: true,
        threshold: 10,
        keyboard: { enabled: true },
        
        pagination: dotsEl ? {
          el: dotsEl,
          clickable: true,
          bulletClass: 'tctl-dot',
          bulletActiveClass: 'active'
        } : false,
        

        navigation: (prevEl && nextEl) ? {
          nextEl: nextEl,
          prevEl: prevEl,
        } : false,
        
        autoplay: doAuto ? {
          delay: spd,
          disableOnInteraction: false,
          stopOnLastSlide: false,
          pauseOnMouseEnter: true,
        } : false,

        breakpoints: {
          0:   { slidesPerView: 1.2, spaceBetween: 16 },
          768: { slidesPerView: 1.8, spaceBetween: 20 },
          1024: { slidesPerView: 3,   spaceBetween: 24 }
        }
      });
    });
  }

  function initPortfolioCarousel() {
    document.querySelectorAll('.vista-pf-carousel-wrap').forEach(function (wrap) {
      if (wrap._pfcInit) return;
      wrap._pfcInit = true;

      var swiperEl = wrap.querySelector('.pfc-swiper');
      if (!swiperEl || typeof window.Swiper === 'undefined') return;

      var spv    = parseInt(wrap.getAttribute('data-spv'))  || 3;
      var gap    = parseInt(wrap.getAttribute('data-gap'))   || 25;
      var doAuto = wrap.getAttribute('data-autoplay') === 'true';
      var spd    = parseInt(wrap.getAttribute('data-speed')) || 4000;
      var doLoop = wrap.getAttribute('data-loop') === 'true';

      var dotsEl = wrap.querySelector('.pfc-dots');
      var prevEl = wrap.querySelector('.pfc-prev');
      var nextEl = wrap.querySelector('.pfc-next');

      new window.Swiper(swiperEl, {
        loop:         doLoop,
        speed:        600,
        grabCursor:   true,
        threshold:    10,
        keyboard:     { enabled: true },
        spaceBetween: gap,

        slidesPerView: 1,
        breakpoints: {
          0:   { slidesPerView: 1,                spaceBetween: 16 },
          600: { slidesPerView: Math.min(spv, 2), spaceBetween: gap },
          960: { slidesPerView: spv,              spaceBetween: gap },
        },

        pagination: dotsEl ? {
          el:               dotsEl,
          clickable:        true,
          bulletClass:      'tctl-dot',
          bulletActiveClass:'active',
        } : false,

        navigation: (prevEl && nextEl) ? {
          prevEl: prevEl,
          nextEl: nextEl,
        } : false,

        autoplay: doAuto ? {
          delay:                spd,
          disableOnInteraction: false,
          pauseOnMouseEnter:    true,
        } : false,
      });
    });
  }

  function initBlogCarousel() {
    document.querySelectorAll('.vista-blog-carousel-wrap').forEach(function (wrap) {
      if (wrap._blcInit) return;
      wrap._blcInit = true;

      var swiperEl = wrap.querySelector('.blc-swiper');
      if (!swiperEl || typeof window.Swiper === 'undefined') return;

      var spv    = parseInt(wrap.getAttribute('data-spv'))  || 3;
      var gap    = parseInt(wrap.getAttribute('data-gap'))   || 25;
      var doAuto = wrap.getAttribute('data-autoplay') === 'true';
      var spd    = parseInt(wrap.getAttribute('data-speed')) || 4000;
      var doLoop = wrap.getAttribute('data-loop') === 'true';

      var dotsEl = wrap.querySelector('.blc-dots');
      var prevEl = wrap.querySelector('.blc-prev');
      var nextEl = wrap.querySelector('.blc-next');

      new window.Swiper(swiperEl, {
        loop:         doLoop,
        speed:        600,
        grabCursor:   true,
        threshold:    10,
        keyboard:     { enabled: true },
        spaceBetween: gap,

        slidesPerView: 1,
        breakpoints: {
          0:   { slidesPerView: 1,                spaceBetween: 16 },
          600: { slidesPerView: Math.min(spv, 2), spaceBetween: gap },
          960: { slidesPerView: spv,              spaceBetween: gap },
        },

        pagination: dotsEl ? {
          el:               dotsEl,
          clickable:        true,
          bulletClass:      'tctl-dot',
          bulletActiveClass:'active',
        } : false,

        navigation: (prevEl && nextEl) ? {
          prevEl: prevEl,
          nextEl: nextEl,
        } : false,

        autoplay: doAuto ? {
          delay:                spd,
          disableOnInteraction: false,
          pauseOnMouseEnter:    true,
        } : false,
      });
    });
  }


  function vistaInit() {
    initRv();
    initReveal();
    initSkillBars();
    initCounters();
    initTestimonials();
    initPortfolioCarousel();
    initBlogCarousel();
  }


  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', vistaInit);
  } else {
    vistaInit();
  }
  function hookElementor() {
    if (typeof window.elementorFrontend === 'undefined') return;
    window.elementorFrontend.hooks.addAction('frontend/element_ready/global', function ($el) {
      var wrap = ($el && $el[0]) ? $el[0] : $el;
      if (wrap && wrap.querySelectorAll) {
        wrap.querySelectorAll('.vista-testimonials-wrap').forEach(function (w) {
          delete w._vistaInit;
        });
        wrap.querySelectorAll('.vista-pf-carousel-wrap').forEach(function (w) {
          delete w._pfcInit;
        });
        wrap.querySelectorAll('.vista-blog-carousel-wrap').forEach(function (w) {
          delete w._blcInit;
        });
      }
      vistaInit();
    });
  }

  if (window.elementorFrontend) {
    hookElementor();
  }
  document.addEventListener('elementor/frontend/init', hookElementor);
  window.addEventListener('elementor/frontend/init', hookElementor);

})();