( function( api ) {

	// Extends our custom "example-1" section.
	api.sectionConstructor['plugin-section'] = api.Section.extend( {

		// No events for this type of section.
		attachEvents: function () {},

		// Always make the section active.
		isContextuallyActive: function () {
			return true;
		}
	} );

} )( wp.customize );

function pawfect_frontpage_sections_scroll( section_id ){
    var scroll_section_selector = null;

    var $contents = jQuery('#customize-preview iframe').contents();

    switch ( section_id ) {
        // Header & Navigation
        case 'accordion-section-pawfect_theme_top_header_area':
            scroll_section_selector = "#site-header";
            break;

        // FRONTPAGE SECTIONS - Updated with actual CSS selectors
        case 'accordion-section-pawfect_main_theme_slider':
            scroll_section_selector = ".pawfect-hero-section";
            break;

        case 'accordion-section-pawfect_theme_press_mentions':
            scroll_section_selector = ".pawfect-press-mentions";
            break;

        case 'accordion-section-pawfect_theme_feature_highlight':
            scroll_section_selector = ".pawfect-feature-highlight";
            break;

        case 'accordion-section-pawfect_theme_service':
        case 'accordion-section-pawfect_service_area':
            scroll_section_selector = ".pawfect-why-choose";
            break;

        case 'accordion-section-pawfect_theme_portfolio':
            scroll_section_selector = ".pawfect-portfolio-section";
            break;

        case 'accordion-section-pawfect_theme_featured_collection':
            scroll_section_selector = ".pawfect-featured-collection";
            break;

        case 'accordion-section-pawfect_theme_testimonial':
        case 'accordion-section-pawfect_testimonial_section':
            scroll_section_selector = ".pawfect-bg-pawfect-background";
            break;

        case 'accordion-section-pawfect_theme_mission':
        case 'accordion-section-pawfect_mission_section':
            scroll_section_selector = ".pawfect-mission-section";
            break;

        case 'accordion-section-pawfect_pet_shop_section':
            scroll_section_selector = ".pawfect-shop-by-pets";
            break;

        case 'accordion-section-pawfect_theme_promo_timer':
        case 'accordion-section-pawfect_promo_timer_section':
            scroll_section_selector = ".pawfect-promo-timer";
            break;

        case 'accordion-section-pawfect_theme_crafted':
        case 'accordion-section-pawfect_crafted_section':
            scroll_section_selector = ".pawfect-crafted-section";
            break;

        case 'accordion-section-pawfect_theme_blog':
            scroll_section_selector = ".pawfect-blog-section";
            break;

        case 'accordion-section-pawfect_theme_promo_banner':
        case 'accordion-section-pawfect_promo_banner_section':
            scroll_section_selector = ".pawfect-promo-banner";
            break;

        case 'accordion-section-pawfect_theme_instagram':
        case 'accordion-section-pawfect_instagram_section':
            scroll_section_selector = ".pawfect-instagram-section";
            break;

        case 'accordion-section-pawfect_theme_cta':
        case 'accordion-section-pawfect_cta_section':
            scroll_section_selector = ".pawfect-cta-section";
            break;

        case 'accordion-section-pawfect_theme_woocommerce':
            scroll_section_selector = ".pawfect-woocommerce-section";
            break;

        // LEGACY SUPPORT - Old selector patterns
        case 'accordion-section-pawfect_theme_top_info':
            scroll_section_selector = "#topinfo-selector-scroll";
            break;

        case 'accordion-section-pawfect_theme_funfact':
            scroll_section_selector = "#funfact-selector-scroll";
            break;

        case 'accordion-section-pawfect_theme_team':
            scroll_section_selector = "#team-selector-scroll";
            break;

        case 'accordion-section-pawfect_theme_client':
            scroll_section_selector = "#client-selector-scroll";
            break;

        case 'accordion-section-pawfect_theme_footer_info':
            scroll_section_selector = "#footerinfo-selector-scroll";
            break;
    }

    // Perform the scroll if we found a valid selector
    if( scroll_section_selector ){
        var $target = $contents.find(scroll_section_selector);
        if( $target.length > 0 ){
            $contents.find("html, body").animate({
                scrollTop: $target.offset().top - 50 // Add 50px offset for better visibility
            }, 800); // Smooth 800ms animation
        }
    }
}

// Enhanced Auto-Scroll Functionality for Frontpage Sections
jQuery(document).ready(function($) {

    // Handle clicks on frontpage section titles
    $('body').on('click', '#sub-accordion-panel-pawfect_frontpage_settings .control-subsection .accordion-section-title', function(event) {
        var section_id = $(this).parent('.control-subsection').attr('id');
        // Add small delay to allow section to expand first
        setTimeout(function() {
            pawfect_frontpage_sections_scroll(section_id);
        }, 300);
    });

    // Handle WordPress Customizer API section expansion events
    if (typeof wp !== 'undefined' && wp.customize) {
        wp.customize.bind('ready', function() {

            // Define section mappings for API-based scrolling
            var sectionMappings = {
                'pawfect_main_theme_slider': '.pawfect-hero-section',
                'pawfect_theme_press_mentions': '.pawfect-press-mentions',
                'pawfect_theme_feature_highlight': '.pawfect-feature-highlight',
                'pawfect_theme_service': '.pawfect-why-choose',
                'pawfect_service_area': '.pawfect-why-choose',
                'pawfect_theme_portfolio': '.pawfect-portfolio-section',
                'pawfect_theme_featured_collection': '.pawfect-featured-collection',
                'pawfect_theme_testimonial': '.pawfect-bg-pawfect-background',
                'pawfect_testimonial_section': '.pawfect-bg-pawfect-background',
                'pawfect_theme_mission': '.pawfect-mission-section',
                'pawfect_mission_section': '.pawfect-mission-section',
                'pawfect_pet_shop_section': '.pawfect-shop-by-pets',
                'pawfect_theme_promo_timer': '.pawfect-promo-timer',
                'pawfect_promo_timer_section': '.pawfect-promo-timer',
                'pawfect_theme_crafted': '.pawfect-crafted-section',
                'pawfect_crafted_section': '.pawfect-crafted-section',
                'pawfect_theme_blog': '.pawfect-blog-section',
                'pawfect_theme_promo_banner': '.pawfect-promo-banner',
                'pawfect_promo_banner_section': '.pawfect-promo-banner',
                'pawfect_theme_instagram': '.pawfect-instagram-section',
                'pawfect_instagram_section': '.pawfect-instagram-section',
                'pawfect_theme_cta': '.pawfect-cta-section',
                'pawfect_cta_section': '.pawfect-cta-section',
                'pawfect_theme_woocommerce': '.pawfect-woocommerce-section'
            };

            // Bind to section expansion events
            $.each(sectionMappings, function(sectionId, selector) {
                if (wp.customize.section(sectionId)) {
                    wp.customize.section(sectionId).expanded.bind(function(isExpanded) {
                        if (isExpanded) {
                            // Add delay to ensure preview is ready
                            setTimeout(function() {
                                pawfect_scroll_to_section(selector);
                            }, 500);
                        }
                    });
                }
            });
        });
    }
});

// Helper function for API-based scrolling
function pawfect_scroll_to_section(selector) {
    var $contents = jQuery('#customize-preview iframe').contents();
    var $target = $contents.find(selector);

    if ($target.length > 0) {
        $contents.find("html, body").animate({
            scrollTop: $target.offset().top - 50
        }, 800);
    }
}
