(function ($) {
    wp.customize.bind('ready', function () {

        $('.customize-control[data-home-section]').hide();

        function updateFieldVisibility(section) {
            
            const sourceControl = wp.customize(section + '_source');
            
            if (!sourceControl) return;
            
            const source = sourceControl.get();            
            const categoryControl = wp.customize.control(section + '_category');
            const tagControl = wp.customize.control(section + '_tag');
            const postsControl = wp.customize.control(section + '_posts');
            const excludePostsControl = wp.customize.control(section + '_exclude_posts');            
            
            if (categoryControl) categoryControl.container.toggle(source === 'category');
            if (tagControl) tagControl.container.toggle(source === 'tag');
            if (postsControl) postsControl.container.toggle(source === 'custom_posts');
            if (excludePostsControl) excludePostsControl.container.toggle(source !== 'custom_posts' && source !== 'none');
            
        }

        wp.customize.previewer.bind('aurora-lite-set-active-section', function (data) {
            if (!data || !data.section) return;

            $('.customize-control[data-home-section]').hide();
            $('.customize-control[data-home-section="' + data.section + '"]').show();
            updateFieldVisibility(data.section);
        });

        $('.customize-control[data-home-section]').each(function() {
            const section = $(this).attr('data-home-section');
            const controlId = $(this).attr('id');
            
            if (controlId && controlId.indexOf('customize-control-' + section + '_source') === 0) {
                wp.customize(section + '_source', function(setting) {
                    setting.bind(function() {
                        updateFieldVisibility(section);
                    });
                });
            }
        });

    });

    
})(jQuery);

(function(wp, $) {

    function toggleDeviceControls(device) {
        wp.customize.control.each(function(control) {
            if (control.params.device_controls) {
                const allowedDevices = control.params.device_controls;
                const matchesDevice = allowedDevices.includes(device);                
                const isActive = control.active();                
                control.container.toggle(matchesDevice && isActive);
            }
        });
    }

    wp.customize.bind('ready', function() {
        toggleDeviceControls(wp.customize.previewedDevice.get());
        
        wp.customize.previewedDevice.bind(function(device) {
            toggleDeviceControls(device);
        });

        wp.customize.control.each(function(control) {
            if (control.params.device_controls) {
                control.active.bind(function() {
                    toggleDeviceControls(wp.customize.previewedDevice.get());
                });
            }
        });
    });

})(wp, jQuery);