(function ($) {
    "use strict";

    // Spinner: aguarda todos os recursos carregarem antes de esconder
    $(window).on('load', function () {
        if ($('#spinner').length > 0) {
            $('#spinner').removeClass('show');
        }
    });
    
    
    // Initiate the wowjs
    window.wow = new WOW();
    window.wow.init();


    // Sticky Navbar with Theme Switcher Full Wrapper
    $(window).scroll(function () {
        if ($(this).scrollTop() > 40) {
            $('.navbar').addClass('sticky-top');
            $('.site-header-wrapper').addClass('shadow-sm');
        } else {
            $('.navbar').removeClass('sticky-top');
            $('.site-header-wrapper').removeClass('shadow-sm');
        }
    });
    
    // Dropdown on mouse hover
    const $dropdown = $(".dropdown");
    const $dropdownToggle = $(".dropdown-toggle");
    const $dropdownMenu = $(".dropdown-menu");
    const showClass = "show";
    
    $(window).on("load resize", function() {
        if (this.matchMedia("(min-width: 992px)").matches) {
            $dropdown.hover(
            function() {
                const $this = $(this);
                $this.addClass(showClass);
                $this.find($dropdownToggle).attr("aria-expanded", "true");
                $this.find($dropdownMenu).addClass(showClass);
            },
            function() {
                const $this = $(this);
                $this.removeClass(showClass);
                $this.find($dropdownToggle).attr("aria-expanded", "false");
                $this.find($dropdownMenu).removeClass(showClass);
            }
            );
        } else {
            $dropdown.off("mouseenter mouseleave");
        }
    });
    
    
    // Back to top button
    $(window).scroll(function () {
        if ($(this).scrollTop() > 100) {
            $('.back-to-top').fadeIn('slow');
        } else {
            $('.back-to-top').fadeOut('slow');
        }
    });
    $('.back-to-top').click(function () {
        $('html, body').animate({scrollTop: 0}, 1500, 'easeInOutExpo');
        return false;
    });


    // Date and time picker
    $('.date').datetimepicker({
        format: 'L'
    });
    $('.time').datetimepicker({
        format: 'LT'
    });





    // Price carousel
    var $priceCarousel = $(".price-carousel");
    var priceItemsCount = $priceCarousel.children().length;
    
    $priceCarousel.owlCarousel({
        autoplay: true,
        smartSpeed: 1500,
        margin: 20,
        dots: false,
        loop: priceItemsCount > 2,
        nav : true,
        navText : [
            '<i class="bi bi-arrow-left"></i>',
            '<i class="bi bi-arrow-right"></i>'
        ],
        responsive: {
            0: {
                items: 1,
                loop: priceItemsCount > 1
            },
            768: {
                items: 2,
                margin: 20,
                loop: priceItemsCount > 2
            }
        }
    });


    // Testimonials carousel
    var $testimonialCarousel = $(".testimonial-carousel");
    var testimonialCount = $testimonialCarousel.children().length;
    
    $testimonialCarousel.owlCarousel({
        autoplay: true,
        smartSpeed: 1000,
        items: 1,
        dots: false,
        loop: testimonialCount > 1,
        nav : true,
        navText : [
            '<i class="bi bi-arrow-left"></i>',
            '<i class="bi bi-arrow-right"></i>'
        ],
    });
    // Typewriter Effect with Observer (Handles Carousels and Animations perfectly)
    var initTypewriter = function() {
        var observer = new IntersectionObserver(function(entries) {
            entries.forEach(function(entry) {
                var $ele = $(entry.target);
                var str = $ele.attr('data-text');
                
                if (!str) {
                    str = $ele.text().trim();
                    $ele.attr('data-text', str);
                }
                
                if (entry.isIntersecting) {
                    // Start Typing
                    $ele.text('');
                    $ele.css({ opacity: 1, visibility: 'visible' });
                    
                    var progress = 0;
                    var typingSpeed = parseInt($ele.attr('data-speed') || 80, 10);
                    var delay = parseInt($ele.attr('data-delay') || 500, 10); // Wait for CSS animations
                    
                    if ($ele.data('timer')) clearInterval($ele.data('timer'));
                    if ($ele.data('timeout')) clearTimeout($ele.data('timeout'));
                    
                    var timeout = setTimeout(function() {
                        var timer = setInterval(function() {
                            $ele.text(str.substring(0, progress++));
                            if (progress > str.length) {
                                clearInterval(timer);
                            }
                        }, typingSpeed);
                        $ele.data('timer', timer);
                    }, delay);
                    $ele.data('timeout', timeout);
                } else {
                    // Reset if hidden (e.g. carousel slide changes out)
                    if ($ele.data('timer')) clearInterval($ele.data('timer'));
                    if ($ele.data('timeout')) clearTimeout($ele.data('timeout'));
                    $ele.text(''); 
                }
            });
        }, { threshold: 0.1 });

        $('.typewriter').each(function() {
            if ($(this).is('#titulo-digitando, #texto-digitando')) return;
            observer.observe(this);
        });
    };

    $(document).ready(function() {
        initTypewriter();
    });

})(jQuery);

