(function( $ ){
    /**
     * BlogBeeExtrasConfig is the data passed via PHP
     */

    /**
     * MARK: Preview Refresh
     * Bind with the customizer to detect the currently expanded and collapsed section
     * 
     * @param { Object } sectionInstance    The section instance of the expanded section
     * @since 1.0.0
     */
    wp.customize.state( 'expandedSection' ).bind( function( sectionInstance ){
        const { id } = sectionInstance

        const previewUrl = wp.customize.previewer.previewUrl()

        // Refresh to a random single post on section open
        if( ( id === 'single_section' ) && ( previewUrl != BlogBeeExtrasConfig.single ) ) {
            wp.customize.previewer.previewUrl.set( BlogBeeExtrasConfig.single );
        }

        // Refresh to a random single page on section open
        if( ( id === 'page_section' ) && ( previewUrl != BlogBeeExtrasConfig.page ) ) {
            wp.customize.previewer.previewUrl.set( BlogBeeExtrasConfig.page );
        }

        // Refresh to home on section open
        if( ( id === 'archives_section' ) && ( previewUrl != BlogBeeExtrasConfig.home ) ) {
            wp.customize.previewer.previewUrl.set( BlogBeeExtrasConfig.home );
        }

        // Refresh to search on section open
        if( ( id === 'search_page_section' ) && ( previewUrl != BlogBeeExtrasConfig.search ) ) {
            wp.customize.previewer.previewUrl.set( BlogBeeExtrasConfig.search );
        }

        // Refresh to Error on section open
        if( ( id === 'error_page_settings_section' ) && ( previewUrl != BlogBeeExtrasConfig.error ) ) {
            wp.customize.previewer.previewUrl.set( BlogBeeExtrasConfig.error );
        }
    } )

    /**
     * MARK: Add section classes
     * 
     * @since 1.0.0
     */
    const BbeSections = {
        /**
         * Sections to add the behaviour in
         */
        sections: [ 
            // frontpage sections
            'main_banner_section',
            'main_banner_left_section',
            'main_banner_right_section',
            'carousel_section',
            'category_collection_section',
            'video_playlist_section',
            'three_blocks_section',
            'three_blocks_left_section',
            'three_blocks_center_section',
            'three_blocks_right_section',
            'frontpage_widget_area_two_section',
            'frontpage_widget_area_one_section',
            'frontpage_shortcode_two_section',
            'frontpage_shortcode_one_section',
            'content_sidebar_section',
            'web_stories_section',
            'web_story_options',
            'sticky_header_section',
            'post_list_section',
            'post_grid_section',
            // single sections
            'author_box_section',
            'post_navigation_section',
            'related_posts_section',
            'toc_section',
            // page sections
            'page_toc_section'
        ],
        /**
         * Init
         * 
         * @since 1.0.0
         */
        init: function(){
            this.addThemeClass()
        },
        /**
         * Add theme class
         * 
         * @since 1.0.0
         */
        addThemeClass: function(){
            _.each( this.sections, function( sectionId ){
                const sectionInstance = wp.customize.section( sectionId )
                if( sectionInstance ) sectionInstance.headContainer.addClass( 'bbe-section-hidden' )
            } )
        }
    }

    /**
     * MARK: Ready
     * 
     * @since 1.0.0
     */
    wp.customize.bind( 'ready', function() {
        BbeSections.init()
    } )
})( jQuery )