/**
 * Blog Over Customizer Preview - Dynamic Archive Redirect
 * 
 * When on archive pages, clicking the pencil icon redirects to:
 * - Archive Settings if Archive Override is ON
 * - Blog Settings if Archive Override is OFF
 */
(function ($) {
    'use strict';

    // Wait for Customizer to be ready
    wp.customize.bind('preview-ready', function () {

        // Check if we're on an archive page
        var isArchivePage = document.body.classList.contains('archive') ||
            document.body.classList.contains('category') ||
            document.body.classList.contains('tag') ||
            document.body.classList.contains('author') ||
            document.body.classList.contains('date');

        if (!isArchivePage) {
            return;
        }

        // Listen for messages from parent (Customizer controls)
        wp.customize.preview.bind('active', function () {

            // Intercept clicks on edit shortcuts in archive context
            $(document).on('click', '.customize-partial-edit-shortcut button', function (e) {
                var $shortcut = $(this).closest('.customize-partial-edit-shortcut');
                var partialId = $shortcut.data('customize-partial-id');

                // Check if this is a blog-related partial that should redirect
                var blogPartials = [
                    'Blog_Over_entry_elements',
                    'Blog_Over_entry_meta',
                    'Blog_Over_style',
                    'Blog_Over_layout'
                ];

                // If it's a blog partial on archive page, check override
                if (blogPartials.indexOf(partialId) !== -1) {
                    e.preventDefault();
                    e.stopPropagation();

                    // Send message to parent to focus appropriate section
                    wp.customize.preview.send('blog-over-archive-redirect', {
                        partial: partialId
                    });
                }
            });
        });
    });

})(jQuery);
