/**
 * Customizer Section Shortcuts - Preview Script
 * 
 * Handles two-way communication:
 * 1. Click events on section edit buttons - sends focus requests to panel
 * 2. Scroll requests from panel - scrolls preview to target section
 *
 * @package Paws_And_Care
 * @since 3.0.1
 */

(function ($) {
    'use strict';

    // Wait for Customizer preview to be ready
    if (typeof wp !== 'undefined' && wp.customize && wp.customize.preview) {

        // Listen for scroll-to-section requests from panel
        // This must be bound early, before 'active' event
        wp.customize.preview.bind('scroll-to-section', function (elementSelector) {
            var $target = $(elementSelector);

            if ($target.length) {
                // Smooth scroll to the section
                $('html, body').animate({
                    scrollTop: $target.offset().top - 50 // 50px offset for better visibility
                }, 500, 'swing');
            }
        });

        wp.customize.preview.bind('active', function () {

            // Handle section shortcut clicks (edit icon -> focus panel section)
            $(document).on('click', '.customizer-section-shortcut', function (e) {
                e.preventDefault();
                e.stopPropagation();

                var sectionId = $(this).data('section');

                if (sectionId) {
                    // Send focus request to Customizer panel
                    wp.customize.preview.send('focus-section', sectionId);
                }
            });

        });

    } else {
        // Fallback for when Customizer API isn't ready
        $(document).ready(function () {
            $(document).on('click', '.customizer-section-shortcut', function (e) {
                e.preventDefault();
                e.stopPropagation();

                var sectionId = $(this).data('section');

                if (sectionId && typeof wp !== 'undefined' && wp.customize && wp.customize.preview) {
                    wp.customize.preview.send('focus-section', sectionId);
                }
            });
        });
    }

})(jQuery);


