<?php
/**
 * The sidebar containing the main widget area
 *
 * @package Pawfect
 */

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}
?>

<aside id="secondary" class="pawfect-blog-sidebar pawfect-space-y-6">

    <div class="pawfect-blog-sidebar-widgets">
        <?php 
        $has_widgets = false;
        if (is_active_sidebar('sidebar-1')) {
            ob_start();
            dynamic_sidebar('sidebar-1');
            $sidebar_content = ob_get_clean();
            
            // Check if the sidebar content contains more than just empty block wrappers
            if (trim(strip_tags($sidebar_content)) !== '') {
                echo $sidebar_content;
                $has_widgets = true;
            }
        }
        
        // If strictly no widgets or only empty block wrappers exist, output fallback natively
        if (!$has_widgets): ?>
            
            <div id="search-fallback" class="widget widget_search pawfect-bg-white pawfect-p-8 pawfect-rounded-xl pawfect-shadow-lg pawfect-border pawfect-border-gray-100 pawfect-mb-6">
                <h3 class="pawfect-widget-title pawfect-text-lg pawfect-font-bold pawfect-text-gray-900 pawfect-mb-4 pawfect-pb-2 pawfect-border-b pawfect-border-gray-200"><?php esc_html_e( 'Search', 'pawfect' ); ?></h3>
                <?php get_search_form(); ?>
            </div>
            
            <div id="recent-posts-fallback" class="widget widget_recent_entries pawfect-bg-white pawfect-p-8 pawfect-rounded-xl pawfect-shadow-lg pawfect-border pawfect-border-gray-100 pawfect-mb-6">
                <h3 class="pawfect-widget-title pawfect-text-lg pawfect-font-bold pawfect-text-gray-900 pawfect-mb-4 pawfect-pb-2 pawfect-border-b pawfect-border-gray-200"><?php esc_html_e( 'Recent Posts', 'pawfect' ); ?></h3>
                <ul>
                    <?php wp_get_archives( array( 'type' => 'postbypost', 'limit' => 5 ) ); ?>
                </ul>
            </div>
            
        <?php endif; ?>
    </div>
    
</aside><!-- #secondary -->
