console.log("here");

jQuery(document).ready(() => {
    initNavSubmenus();
    initSyrusSettings(jQuery);

    if(jQuery('input[name=whatsapp_profile_picture_uploader]').length > 0)
        initWhatsappProfilePictureUploader();
})

function initNavSubmenus() {
    jQuery('.section-topnav-menu nav li.with-submenu').on('click',(e) => {
        let $el = jQuery(e.currentTarget);
        let $realTarget = jQuery(e.target);
        let $submenu = jQuery($el.find('.submenu'));

        if($realTarget.parent().get(0) == $submenu.get(0))
            return true;

        e.preventDefault();
        
        jQuery('.section-topnav-menu nav li.with-submenu .submenu').hide();

        jQuery('.section-topnav-menu nav li.with-submenu').each((index,element) => {
            if(element != $el.get(0))
                jQuery(element).removeClass('open');
        });

        if($el.hasClass('open')) {
            $el.removeClass('open');
            $submenu.hide();
            $el.blur();
        } else {
            $el.addClass('open');
            $submenu.show();
        }



    });
}

function initWhatsappProfilePictureUploader() {
    jQuery('input[name=whatsapp_profile_picture_uploader]').on('change', (event) => {
        let file = event.target.files[0];
        let reader = new FileReader();
        
        reader.onloadend = function() {
            let base64String = reader.result;
            jQuery('.whatsapp-profile-picture-thumbnail').attr('src', base64String); 
            jQuery('#form-whatsapp-settings input[name=thumbnail_image]').val(base64String);
        };

        reader.readAsDataURL(file);
    });
}

function initSyrusSettings($) {
    let $sidebar = $('.syrus-settings-sidebar');
    let $sidebarLinks = $('.syrus-sidebar-nav a');
    let $sections = $('.syrus-settings-section');
    let $toggleSwitches = $('.syrus-toggle-switch input[type="checkbox"]');

    if (!$sidebar.length) return;

    $sidebarLinks.on('click', function(e) {
        e.preventDefault();
        
        let targetId = $(this).attr('href').substring(1);
        let $targetSection = $('#' + targetId);
        
        if ($targetSection.length) {
            $sidebarLinks.removeClass('active');
            $(this).addClass('active');
            
            $('html, body').animate({
                scrollTop: $targetSection.offset().top - 50
            }, 500);
        }
    });

    let observer = new IntersectionObserver(function(entries) {
        entries.forEach(function(entry) {
            if (entry.isIntersecting && entry.intersectionRatio > 0.5) {
                let $activeLink = $('.syrus-sidebar-nav a[href="#' + entry.target.id + '"]');
                if ($activeLink.length) {
                    $sidebarLinks.removeClass('active');
                    $activeLink.addClass('active');
                }
            }
        });
    }, { threshold: [0.5] });

    $sections.each(function() {
        observer.observe(this);
    });

    if ($sidebarLinks.length > 0) {
        $sidebarLinks.first().addClass('active');
    }

    $('.syrus-toggle-slider').on('click', function(e) {
        e.preventDefault();
        let $checkbox = $(this).prev('input[type="checkbox"]');
        if ($checkbox.length) {
            $checkbox.prop('checked', !$checkbox.is(':checked'));
            $checkbox.trigger('change');
        }
    });

    $toggleSwitches.on('change', function() {
        let $slider = $(this).next('.syrus-toggle-slider');
        if ($slider.length) {
            if ($(this).is(':checked')) {
                $slider.css('transform', 'scale(1.05)');
                setTimeout(function() {
                    $slider.css('transform', 'scale(1)');
                }, 150);
            } else {
                $slider.css('transform', 'scale(0.95)');
                setTimeout(function() {
                    $slider.css('transform', 'scale(1)');
                }, 150);
            }
        }
    });
}
         