<?php
    /**
     * Template part for reordering sidebars in single || index || page || archive
     *
     * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
     *
     * @package Blog Bee
     * @since 1.0.0
     */
    use BBE\Helpers as Helpers;
    use BBE\Customizer\Theme_Defaults as Theme_Defaults;

    if( is_single() ) {

        $sidebar_reorder = Helpers::check_single_sidebar_post_meta( 'sidebar_options_single_sidebar' );
        $sticky_option = Theme_Defaults::get_theme_option( 'sidebar_options_single_sticky_option' );

    } elseif ( is_page() || is_404() ) {
        
        $sidebar_reorder = Helpers::check_single_sidebar_post_meta( 'sidebar_options_page_sidebar' );
        $sticky_option = Theme_Defaults::get_theme_option( 'sidebar_options_page_sticky_option' );

    } elseif ( is_front_page() && ! is_home() ) {

        $sidebar_reorder = Theme_Defaults::get_theme_option( 'sidebar_options_frontpage_sidebar' );
        $sticky_option = Theme_Defaults::get_theme_option( 'sidebar_options_frontpage_sticky_option' );
        
    } elseif( is_search() ) {
    
        $sidebar_reorder = Theme_Defaults::get_theme_option( 'sidebar_options_search_sidebar' );
        $sticky_option = Theme_Defaults::get_theme_option( 'sidebar_options_search_sticky_option' );

    } else {

        $sidebar_reorder = Helpers::check_single_sidebar_post_meta( 'sidebar_options_archive_sidebar' );
        $sticky_option = Theme_Defaults::get_theme_option( 'sidebar_options_archive_sticky_option' );
        
    }
        
    $content = isset( $args[ 'content' ] ) ? $args[ 'content' ] : '';
    $block_class[] = 'content-wrapper';
    if( $sticky_option ) $block_class[] = 'is-sticky';

    echo '<div class="' . esc_attr( implode( ' ', $block_class ) ) . '">';

        if( ! empty( $sidebar_reorder ) && is_array( $sidebar_reorder ) ) {

            foreach( $sidebar_reorder as $sidebar ) {

                if( ! $sidebar[ 'value' ] ) continue;
                
                switch( $sidebar[ 'id' ] ) {

                    case 'left':
                            get_sidebar( 'left' );
                        break;
                        
                    case 'right':
                            get_sidebar();
                        break;

                    default:
                            echo $content;
                        break;

                }

            }
            
        }

    echo '</div><!-- .content-wrapper -->';