<?php
/**
 * The main template file
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package Blog Bee
 */
use BBE\Helpers as Helpers;
use BBE\Query_Manager as Query_Manager;
use BBE\Customizer\Section\Archives as Archives;
use BBE\Customizer\Section\Pagination as Pagination;
use BBE\Customizer\Theme_Defaults as Theme_Defaults;
use BBE\Customizer\Section\Advertisement as Advertisement;

$archives = Archives::get_instance();
$masonry = $archives->get_masonry();
$layout = $archives->get_layout();

get_header();

	ob_start();

		Query_Manager::get_archive_opener();

			ob_start();
			
				?>

					<main id="primary" class="site-main" <?php Helpers::the_aos_data_attr(); ?>>

						<?php
							if ( have_posts() ) :

								if ( is_home() && ! is_front_page() ) :
									?>
										<header>
											<h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
										</header>
									<?php
								endif;

								Archives::get_instance()->render_section_title();

								echo '<div class="article-wrap">';

									if( ( $layout !== 'list' ) && $masonry ) echo '<div class="bbe-masonry-sizer"></div>';

									Advertisement::get_instance()->prepare_random_ads();

									/* Start the Loop */
									while ( have_posts() ) :
										the_post();

										Advertisement::get_instance()->get_random_advertisement();

										/*
										* Include the Post-Type-specific template for the content.
										* If you want to override this in a child theme, then include a file
										* called content-___.php (where ___ is the Post Type name) and that will be used instead.
										*/
										get_template_part( 'template-parts/content', get_post_type() );

									endwhile;

								echo '</div><!-- .article-wrap -->';
									
								Pagination::get_instance()->render_html();

							else :

								get_template_part( 'template-parts/content', 'none' );

							endif;
						?>

					</main><!-- #primary -->

				<?php
				$content = ob_get_clean();

				$sidebar_args = [
					'content'	=>	$content
				];

				get_template_part( 'template-parts/content', 'sidebar-reorder', $sidebar_args );	// For sidebar reorder

		Query_Manager::get_archive_closer();
	
	$index = ob_get_clean();

	$template_args = [
		'default'	=>	$index
	];

	get_template_part( 'template-parts/content', 'section-reorder', $template_args );	// For section reorder

get_footer();
