<?php
/**
 * Class Loader and Initializer for the Wira theme.
 *
 * This class loads all necessary class files and initializes them.
 *
 * @package    Wira
 * @since      1.0.0
 */

namespace Wira;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Main Class Loader for the theme.
 *
 * Implements a Singleton pattern to load and initialize
 * all required theme classes.
 *
 * @since 1.0.0
 */
class Class_Loader {

	/**
	 * Singleton instance.
	 *
	 * @since 1.0.0
	 * @var self|null
	 */
	private static $instance = null;

	/**
	 * Get the Singleton instance.
	 *
	 * @since 1.0.0
	 * @return self
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Private constructor to enforce Singleton.
	 *
	 * Loads and initializes theme classes.
	 *
	 * @since 1.0.0
	 */
	private function __construct() {
		$this->load_classes();
		$this->init_classes();
	}

	/**
	 * Prevent cloning of the instance.
	 *
	 * @since 1.0.0
	 * @return void
	 */
	private function __clone() {}

	/**
	 * Prevent unserialization of the instance.
	 *
	 * @since 1.0.0
	 * @throws \Exception If unserialization is attempted.
	 * @return void
	 */
	public function __wakeup() {
		throw new \Exception( 'Cannot unserialize singleton' );
	}

	/**
	 * Load all required class files.
	 *
	 * Iterates through the predefined class map and requires
	 * PHP files if they exist.
	 *
	 * @since 1.0.0
	 * @return void
	 */
	private function load_classes() {
		$classes = array(
			'Theme_Setup'          => 'inc/class/class-theme-setup.php',
			'Setup_Page'           => 'inc/class/class-setup-page.php',
			'Register_Sidebar'     => 'inc/class/class-sidebar-manager.php',
			'Assets_Enqueue'       => 'inc/class/class-assets-enqueue.php',
			'Customizer'           => 'inc/class/customizer/class-customizer.php',
			'Inline_Style'         => 'inc/class/class-inline-style.php',
			'Elementor'            => 'inc/class/compatibility/class-elementor-compatibility.php',
			'Register_Block_Style' => 'inc/class/compatibility/class-register-block-style.php',
			'Custom_Comments'      => 'inc/class/class-custom-comments.php',
			'Bootstrap_Navwalker'  => 'inc/class/class-bootstrap-navwalker.php',
			'Menu_Fallback'        => 'inc/class/class-menu-fallback.php',
			'Custom_Pagination'    => 'inc/class/templates/class-custom-pagination.php',
			'Post_Navigation'      => 'inc/class/templates/class-post-navigation.php',
			'Post_Tags'            => 'inc/class/templates/class-post-tags.php',
			'Scroll_Top'           => 'inc/class/templates/class-scroll-top.php',
			'Preloader'            => 'inc/class/templates/class-preloader.php',
		);

		foreach ( $classes as $class => $path ) {
			$full_path = WIRA_DIR . $path;

			if ( file_exists( $full_path ) ) {
				require_once $full_path;
			}
		}
	}

	/**
	 * Initialize theme classes.
	 *
	 * Instantiates or retrieves Singleton instances
	 * for required classes.
	 *
	 * @since 1.0.0
	 * @return void
	 */
	private function init_classes() {
		// Singleton classes.
		\Wira\Theme_Setup::get_instance();
		\Wira\Setup_Page::get_instance();

		// General classes.
		new \Wira\Assets_Enqueue();
		new \Wira\Customizer();
		new \Wira\Inline_Style();
		new \Wira\Register_Block_Style();
		new \Wira\Sidebar_Manager();
		new \Wira\Custom_Comments();
	}
}
