<?php
/**
 * The sidebar template file
 *
 * This template displays the primary sidebar widget area.
 * If no widgets are active, the sidebar will not be displayed.
 *
 * @package ROI_Theme
 * @since 1.0.0
 */

// Exit if the sidebar is not active
if ( ! is_active_sidebar( 'sidebar-1' ) ) {
	return;
}
?>

<div class="sidebar-sticky position-sticky" style="top: 5rem;">
	<?php
	/**
	 * Display Table of Contents (TOC) on single posts
	 * Issue #86 - TOC should be displayed in sidebar
	 * Clean Architecture: Usa TableOfContentsRenderer
	 */
	if (is_single() && function_exists('roi_render_component')) {
		echo roi_render_component('table-of-contents'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	}
	?>

	<?php
	/**
	 * Display sidebar widgets
	 *
	 * Widgets can be added through Appearance > Widgets in the WordPress admin.
	 * The sidebar must be registered in functions.php for widgets to appear here.
	 */
	if (is_active_sidebar('sidebar-1')) {
		dynamic_sidebar('sidebar-1');
	}
	?>

	<?php
	/**
	 * CTA Box Sidebar (Issue #36)
	 *
	 * Display CTA box below TOC on single posts
	 */
	get_template_part( 'template-parts/cta-box', 'sidebar' );
	?>
</div><!-- .sidebar-sticky -->
