<?php
/**
 * Gets the root component based object by its class name.
 *
 * @package EaLab\WP_Deployer
 */

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

use EaLab\WP_Deployer\vendor\Dakwamine\Component\ComponentBasedObject;
use EaLab\WP_Deployer\vendor\Dakwamine\Component\Exception\UnmetDependencyException;
use EaLab\WP_Deployer\vendor\Ealab\Deployer\Exception\WpDeployException;


/**
 * Gets the root component based object by its class name.
 *
 * @template T
 *
 * @param class-string<T>|string $class_name The class name.
 * @param bool                   $throw_on_error Whether to throw an exception on error or not.
 *
 * @return T|null The root component based object.
 *
 * @throws WpDeployException Could not get the root component based object.
 *   Throws an exception if $throw_on_error is true.
 */
function deploy_sync_content__get_root_cbo( string $class_name, bool $throw_on_error = true ): ?ComponentBasedObject {
	try {
		return ComponentBasedObject::getRootComponentByClassName( $class_name, true );
	} catch ( UnmetDependencyException $e ) {
		if ( $throw_on_error ) {
			throw new WpDeployException( 'Could not get the root component based object of Deploy & Sync Content. Please make sure the plugin was properly installed.' );
		}

		return null;
	}
}
