<?php
/**
 * @license GPL-2.0-or-later
 * @link    https://wordpress.org/plugins/pressio-crm
 */
namespace PressioCRM\Integrations;

defined( 'ABSPATH' ) || exit;

class IntegrationManager {

	/** @var array<string, object> Registered integrations keyed by slug. */
	private array $integrations = [];

	/**
	 * Register an integration with the manager.
	 *
	 * Required interface for $integration:
	 *   slug(): string — unique identifier used as the array key and type slug
	 *   register(): void — called once here; all hooks must be registered inside it
	 *
	 * Pro add-ons can register custom integrations by hooking into
	 * `pressio_crm_register_integration` and calling this method on the
	 * passed manager instance.
	 */
	public function register( object $integration ): void {
		$slug = $integration->slug();
		$this->integrations[ $slug ] = $integration;
		$integration->register();
	}

	/** @return array<string, object> */
	public function get_all(): array {
		return $this->integrations;
	}

	public function get( string $slug ): ?object {
		return $this->integrations[ $slug ] ?? null;
	}

	public function has( string $slug ): bool {
		return isset( $this->integrations[ $slug ] );
	}
}
