<?php
/**
 * RSM Suite shared bootstrap — loaded by each rsm-* plugin.
 *
 * @package Rsmspark_Suite
 */

defined( 'ABSPATH' ) || exit;

$bootstrap_suite_dir = dirname( __FILE__ ) . '/';

if ( ! defined( 'RSMSPARK_SUITE_DIR' ) ) {
	define( 'RSMSPARK_SUITE_DIR', $bootstrap_suite_dir );
}

require_once RSMSPARK_SUITE_DIR . 'includes/rsm-suite-submenu-compat.php';

$runtime_compat_file = $bootstrap_suite_dir . 'includes/rsm-suite-runtime-compat.php';
if ( is_readable( $runtime_compat_file ) ) {
	require_once $runtime_compat_file;
}

if ( ! defined( 'RSMSPARK_SUITE_VERSION' ) ) {
	define( 'RSMSPARK_SUITE_VERSION', '1.9.0' );
}

if ( ! defined( 'RSMSPARK_SUITE_URL' ) ) {
	define(
		'RSMSPARK_SUITE_URL',
		trailingslashit( plugins_url( '/', RSMSPARK_SUITE_DIR . 'bootstrap.php' ) )
	);
}

// Always load the Rsmspark_* shared stack when absent. Do NOT alias old RSM_Suite →
 // Rsmspark_Suite: that skips requiring Migration/helpers and fatals on Rsmspark_Migration::init()
 // when older Spark/Menu/Forms already defined RSM_Suite from a pre-rename shared copy.
if ( ! class_exists( 'Rsmspark_Suite', false ) ) {
	require_once RSMSPARK_SUITE_DIR . 'includes/class-rsmspark-suite.php';
	require_once RSMSPARK_SUITE_DIR . 'includes/class-rsmspark-settings-page.php';
	require_once RSMSPARK_SUITE_DIR . 'includes/class-rsmspark-settings-tabs.php';
	require_once RSMSPARK_SUITE_DIR . 'includes/class-rsmspark-advanced-settings.php';
	require_once RSMSPARK_SUITE_DIR . 'includes/class-rsmspark-migration.php';
	require_once RSMSPARK_SUITE_DIR . 'includes/class-rsmspark-admin-header.php';
}

$embed_chrome_file = $bootstrap_suite_dir . 'includes/class-rsmspark-embed-chrome.php';
if ( is_readable( $embed_chrome_file ) && ! class_exists( 'Rsmspark_Embed_Chrome', false ) ) {
	require_once $embed_chrome_file;
}

if ( class_exists( 'Rsmspark_Embed_Chrome', false ) ) {
	Rsmspark_Embed_Chrome::init();
}

$suite_admin_notice_file = $bootstrap_suite_dir . 'includes/class-rsmspark-suite-admin-notice.php';
if ( ! class_exists( 'Rsmspark_Suite_Admin_Notice', false ) && is_readable( $suite_admin_notice_file ) ) {
	require_once $suite_admin_notice_file;
}

$getting_started_file = $bootstrap_suite_dir . 'includes/class-rsmspark-getting-started-notice.php';
if ( ! class_exists( 'Rsmspark_Getting_Started_Notice', false ) && is_readable( $getting_started_file ) ) {
	require_once $getting_started_file;
}

// Temporary hard-cut dogfood aliases: new Rsmspark_* names → legacy RSM_* when absent.
$rsmspark_suite_dogfood_aliases = array(
	'Rsmspark_Suite'                     => 'RSM_Suite',
	'Rsmspark_Suite_Admin_Notice'        => 'RSM_Suite_Admin_Notice',
	'Rsmspark_Admin_Header'              => 'RSM_Admin_Header',
	'Rsmspark_Settings_Page'             => 'RSM_Settings_Page',
	'Rsmspark_Settings_Tabs'             => 'RSM_Settings_Tabs',
	'Rsmspark_Migration'                 => 'RSM_Migration',
	'Rsmspark_Getting_Started_Notice'    => 'RSM_Getting_Started_Notice',
	'Rsmspark_Advanced_Settings'         => 'RSM_Advanced_Settings',
	'Rsmspark_License_Client'            => 'RSM_License_Client',
	'Rsmspark_Embed_Chrome'              => 'RSM_Embed_Chrome',
	'Rsmspark_Block_Supports'            => 'RSM_Block_Supports',
	'Rsmspark_Block_Editor_Branding'     => 'RSM_Block_Editor_Branding',
	'RSMSPARK_RSF_Settings'              => 'RSM_RSF_Settings',
	'Rsmspark_Suite_SiteOrigin_Branding' => 'RSM_Suite_SiteOrigin_Branding',
	'Rsmspark_Panels_Branding'           => 'RSM_Panels_Branding',
	'Rsmspark_Pro_Free_Swap_Helper'      => 'RSM_Pro_Free_Swap_Helper',
);

foreach ( $rsmspark_suite_dogfood_aliases as $new_class => $old_class ) {
	if ( class_exists( $new_class, false ) && ! class_exists( $old_class, false ) ) {
		class_alias( $new_class, $old_class );
	}
}

if ( ! defined( 'RSMSPARK_SUITE_BOOTSTRAP_DONE' ) ) {
	define( 'RSMSPARK_SUITE_BOOTSTRAP_DONE', true );
	if ( class_exists( 'Rsmspark_Suite', false ) ) {
		Rsmspark_Suite::init();
	}
	if ( class_exists( 'Rsmspark_Migration', false ) ) {
		Rsmspark_Migration::init();
	}
}

if ( class_exists( 'Rsmspark_Suite_Admin_Notice', false ) ) {
	Rsmspark_Suite_Admin_Notice::init();
}

if ( class_exists( 'Rsmspark_Getting_Started_Notice', false ) ) {
	Rsmspark_Getting_Started_Notice::init();
}

$block_editor_branding = $bootstrap_suite_dir . 'includes/class-rsmspark-block-editor-branding.php';
if ( is_readable( $block_editor_branding ) && ! class_exists( 'Rsmspark_Block_Editor_Branding', false ) ) {
	require_once $block_editor_branding;
}

if ( class_exists( 'Rsmspark_Block_Editor_Branding', false ) ) {
	Rsmspark_Block_Editor_Branding::init();
	if ( ! class_exists( 'RSM_Block_Editor_Branding', false ) ) {
		class_alias( 'Rsmspark_Block_Editor_Branding', 'RSM_Block_Editor_Branding' );
	}
}

$spark_news_loader = $bootstrap_suite_dir . 'includes/rsmspark-news-loader.php';
if ( ! function_exists( 'rsmspark_suite_load_spark_news_module' ) && is_readable( $spark_news_loader ) ) {
	require_once $spark_news_loader;
}

add_action(
	'init',
	static function () {
		if (
			! class_exists( 'SiteOrigin_Panels', false )
			&& ! defined( 'SITEORIGIN_PANELS_VERSION' )
			&& ! class_exists( 'SiteOrigin_Widget', false )
		) {
			return;
		}

		$branding = RSMSPARK_SUITE_DIR . 'includes/class-rsmspark-panels-branding.php';
		if ( is_readable( $branding ) && ! class_exists( 'Rsmspark_Panels_Branding', false ) ) {
			require_once $branding;
		}

		if ( class_exists( 'Rsmspark_Panels_Branding', false ) ) {
			if ( method_exists( 'Rsmspark_Panels_Branding', 'ensure_suite_branding' ) ) {
				Rsmspark_Panels_Branding::ensure_suite_branding();
			}
		} elseif ( is_readable( RSMSPARK_SUITE_DIR . 'includes/class-rsmspark-suite-siteorigin-branding.php' ) ) {
			require_once RSMSPARK_SUITE_DIR . 'includes/class-rsmspark-suite-siteorigin-branding.php';
			if ( class_exists( 'Rsmspark_Suite_SiteOrigin_Branding', false ) ) {
				Rsmspark_Suite_SiteOrigin_Branding::init();
				if ( ! class_exists( 'RSM_Suite_SiteOrigin_Branding', false ) ) {
					class_alias( 'Rsmspark_Suite_SiteOrigin_Branding', 'RSM_Suite_SiteOrigin_Branding' );
				}
			}
		}
	},
	15
);


