<?php
/**
 * ServiceProvider class.
 *
 * @package DokanKits\Core\DI\Providers
 * @since   3.0.0
 */

namespace DokanKits\Core\DI\Providers;

use DokanKits\Core\DI\BootableServiceProvider;

/**
 * ServiceProvider Class
 *
 * Manages the registration and booting of Dokan Kits' core services within the container.
 * This service provider handles the core services with the Dokan Kits'
 * dependency injection container.
 *
 * @since   3.0.0
 * @package DokanKits\Core\DI\Providers
 */
class ServiceProvider extends BootableServiceProvider {
	/**
	 * Tag for services added to the container.
	 */
	public const TAG = 'container-service';

	/**
	 * Boot the service provider and register all sub-providers.
	 *
	 * @inheritDoc
	 *
	 * @return void
	 */
	public function boot(): void {
		$this->getContainer()->addServiceProvider( new AdminDashboardServiceProvider() );
		$this->getContainer()->addServiceProvider( new AdminServiceProvider() );
		$this->getContainer()->addServiceProvider( new CoreServiceProvider() );
		$this->getContainer()->addServiceProvider( new DataStoreServiceProvider() );
		$this->getContainer()->addServiceProvider( new FeaturesServiceProvider() );
		$this->getContainer()->addServiceProvider( new MigrationServiceProvider() );
		$this->getContainer()->addServiceProvider( new RestServiceProvider() );
		$this->getContainer()->addServiceProvider( new SetupServiceProvider() );

		/**
		 * Action to register service providers.
		 *
		 * This action allows other plugins or themes to register their own service providers
		 * with the Dokan Kits container.
		 *
		 * @since 3.0.0
		 */
		do_action( 'dokan_kits_register_service_providers', $this );
	}

	/**
	 * {@inheritDoc}
	 *
	 * Check if the service provider can provide the given service alias.
	 *
	 * @param string $alias The service alias to check.
	 *
	 * @return bool True if the service provider can provide the service, false otherwise.
	 */
	public function provides( string $alias ): bool {
		if ( isset( $this->services[ $alias ] ) ) {
			return true;
		}

		return parent::provides( $alias );
	}

	/**
	 * Register the classes.
	 */
	public function register(): void {
		$this->getContainer()->addShared( 'container', $this->getContainer() )->addTag( self::TAG );
		$this->getContainer()->addShared( 'plugin', dokan_kits() )->addTag( self::TAG );
	}
}
