

(function (wp, $) {

    wp.domReady(function () {

        function initCarousel() {

            $('.product-outer-box .wc-block-product-template').each(function () {

                const $this = $(this);

                // Prevent duplicate init
                if (!$this.hasClass('owl-loaded')) {

                    $this.addClass('owl-carousel');

                    $this.owlCarousel({
                        loop: true,
                        margin: 5,
                        nav: true,
                        dots: false,
                        animateOut: 'fadeOut',
                        animateIn: 'fadeIn',
                        smartSpeed: 600,
                        autoplay: true,
                        items: 1,
                        navText: [
                            "<i class='fa-solid fa-angles-left'></i>",
                            "<i class='fa-solid fa-angles-right'></i>"
                        ]
                    });

                }

            });

        }

        // Initial
        initCarousel();

        // Watch Gutenberg dynamic rendering
        const observer = new MutationObserver(function () {
            initCarousel();
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });

    });

})(window.wp, jQuery);

jQuery(document).ready(function() {


  jQuery('.banner-section .owl-carousel').owlCarousel({
    loop: true,
    margin: 5,
    nav: true,
    animateOut: 'fadeOut',
    animateIn: 'fadeIn',
    smartSpeed: 600,
    navText: ["<i class='fa-solid fa-angles-left'></i>", "<i class='fa-solid fa-angles-right'></i>"],
    dots: false,
    rtl: false,
    responsive: {
      0: {
        items: 1,
      },
      600: {
        items: 1
      },
      992: {
        items: 1,
      },
      1200: {
        items: 1,
      }
    },
    autoplay: true,
  });

  jQuery('.product-outer-box .owl-carousel').owlCarousel({
    loop: true,
    margin: 5,
    nav: true,
    animateOut: 'fadeOut',
    animateIn: 'fadeIn',
    smartSpeed: 600,
    navText: ["<i class='fa-solid fa-angles-left'></i>", "<i class='fa-solid fa-angles-right'></i>"],
    dots: false,
    rtl: false,
    responsive: {
      0: {
        items: 1,
      },
      600: {
        items: 1
      },
      992: {
        items: 1,
      },
      1200: {
        items: 1,
      }
    },
    autoplay: true,
  });
});

// Scroll to Top
var cctv_services_scrollBtn = document.querySelector('.scroll-top-btn');
var cctv_services_scrollBtnLink = document.querySelector('.scroll-top-btn a');

if ( cctv_services_scrollBtn ) {
  window.onscroll = function() {
    if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) {
      cctv_services_scrollBtn.style.display = 'block';
    } else {
      cctv_services_scrollBtn.style.display = 'none';
    }
  };
}

if ( cctv_services_scrollBtnLink ) {
  cctv_services_scrollBtnLink.onclick = function(event) {
    event.preventDefault();
    window.scrollTo({top: 0, behavior: 'smooth'});
  };
}


jQuery(function($){

    $('.dynamic-product-section .wc-block-product').each(function(){

        if(!$(this).find('.custom-rating-badge').length){

            let ratingText = $(this)
                .find('.wc-block-grid__product-rating, .wc-block-components-product-rating')
                .text()
                .trim();

            // Extract only rating number
            let match = ratingText.match(/Rated\s([0-9.]+)/);

            if(match && match[1]){

                let rating = match[1];

                $(this).append(
                    '<div class="custom-rating-badge"><i class="fa-solid fa-star"></i> ' +
                    rating +
                    '/5</div>'
                );

            }

        }

    });

});