<?php
/**
 * Widget bootstrap — registers shortcodes and blocks.
 *
 * @package Rsmspark_Content_Widgets
 */

defined( 'ABSPATH' ) || exit;

/**
 * Rsmspark_Content_Widgets_Bootstrap
 *
 * Refactored in compat branch - Phase 1–2
 */
class Rsmspark_Content_Widgets_Bootstrap {

	/**
	 * @var bool
	 */
	private static $classes_loaded = false;

	/**
	 * @var bool
	 */
	private static $registered = false;

	/**
	 * @return void
	 */
	public static function init() {
		self::load_classes();
		add_action( 'init', array( __CLASS__, 'register_all' ), 5 );
		add_action( 'init', array( __CLASS__, 'register_all' ), 15 );
	}

	/**
	 * Load render-layer classes (required before SiteOrigin widgets reference them).
	 *
	 * @return void
	 */
	public static function load_classes() {
		if ( self::$classes_loaded ) {
			return;
		}

		self::$classes_loaded = true;

		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/widgets/class-rsmspark-content-widgets-widget-hero.php';
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/widgets/class-rsmspark-content-widgets-widget-features.php';
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/widgets/class-rsmspark-content-widgets-widget-tabs.php';
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/widgets/class-rsmspark-content-widgets-widget-accordion.php';
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/widgets/class-rsmspark-content-widgets-widget-icon-box-grid.php';
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/widgets/class-rsmspark-content-widgets-widget-testimonials-team.php';
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/widgets/class-rsmspark-content-widgets-widget-image.php';
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/widgets/class-rsmspark-content-widgets-widget-headline.php';
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/widgets/class-rsmspark-content-widgets-widget-buttons.php';
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/widgets/class-rsmspark-content-widgets-widget-footer.php';
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/widgets/class-rsmspark-content-widgets-widget-logo-grid.php';
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/widgets/class-rsmspark-content-widgets-widget-stats-counter.php';
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/widgets/class-rsmspark-content-widgets-widget-event-countdown.php';
		require_once RSMSPARK_CONTENT_WIDGETS_PATH . 'includes/widgets/class-rsmspark-content-widgets-widget-svg-icon.php';
	}

	/**
	 * @return void
	 */
	public static function register_all() {
		if ( self::$registered ) {
			if (
				class_exists( 'Rsmspark_Content_Widgets_Blocks', false )
				&& Rsmspark_Content_Widgets_Blocks::has_block_definitions()
			) {
				return;
			}
		} else {
			self::$registered = true;
		}

		self::load_classes();

		Rsmspark_Content_Widgets_Widget_Hero::register();
		Rsmspark_Content_Widgets_Widget_Features::register();
		Rsmspark_Content_Widgets_Widget_Tabs::register();
		Rsmspark_Content_Widgets_Widget_Accordion::register();
		Rsmspark_Content_Widgets_Widget_Icon_Box_Grid::register();
		Rsmspark_Content_Widgets_Widget_Testimonials_Team::register();
		Rsmspark_Content_Widgets_Widget_Image::register();
		Rsmspark_Content_Widgets_Widget_Headline::register();
		Rsmspark_Content_Widgets_Widget_Buttons::register();
		Rsmspark_Content_Widgets_Widget_Footer::register();
		Rsmspark_Content_Widgets_Widget_Logo_Grid::register();
		Rsmspark_Content_Widgets_Widget_Stats_Counter::register();
		Rsmspark_Content_Widgets_Widget_Event_Countdown::register();
		Rsmspark_Content_Widgets_Widget_Svg_Icon::register();

		if ( class_exists( 'Rsmspark_Content_Widgets_Features_Popouts', false ) ) {
			Rsmspark_Content_Widgets_Features_Popouts::register_shortcodes();
		} else {
			Rsmspark_Content_Widgets_Widget_Features::ensure_dependencies();
			Rsmspark_Content_Widgets_Features_Popouts::register_shortcodes();
		}

		do_action( 'rsmspark_content_widgets_widgets_registered' );
	}
}
