<?php

namespace Limb_Chatbot\Includes\Services\Channel_Events;

/**
 * Bootstraps the channel-event system and provides shared service instances.
 *
 * Acts as a simple service locator / factory so that features across the
 * plugin can obtain a pre-wired {@see Channel_Event_Dispatcher} without
 * constructing the dependency chain themselves.
 *
 * Called once from the main plugin class via
 * {@see Limb_Chatbot::channel_events_init()}.
 *
 * @package Limb_Chatbot
 * @subpackage Services\Channel_Events
 * @since 2.1.0
 */
class Channel_Event_Bootstrap {

	/**
	 * Shared repository instance (lazy).
	 *
	 * @var Channel_Event_Repository|null
	 */
	private static ?Channel_Event_Repository $repository = null;

	/**
	 * Shared dispatcher instance (lazy).
	 *
	 * @var Channel_Event_Dispatcher|null
	 */
	private static ?Channel_Event_Dispatcher $dispatcher = null;

	/**
	 * No public instantiation.
	 */
	private function __construct() {}

	/**
	 * Initialise the channel-event system.
	 *
	 * Currently a no-op beyond ensuring instances are created on first access,
	 * but kept as an explicit boot step so future hooks can be registered here.
	 *
	 * @return void
	 */
	public static function init(): void {
		// Eagerly instantiate so the dispatcher is available immediately for
		// any listener that registers hooks during the same init pass.
		self::get_dispatcher();
	}

	/**
	 * Return the shared {@see Channel_Event_Dispatcher} instance.
	 *
	 * @return Channel_Event_Dispatcher
	 */
	public static function get_dispatcher(): Channel_Event_Dispatcher {
		if ( self::$dispatcher === null ) {
			self::$dispatcher = new Channel_Event_Dispatcher( self::get_repository() );
		}

		return self::$dispatcher;
	}

	/**
	 * Return the shared {@see Channel_Event_Repository} instance.
	 *
	 * @return Channel_Event_Repository
	 */
	public static function get_repository(): Channel_Event_Repository {
		if ( self::$repository === null ) {
			self::$repository = new Channel_Event_Repository();
		}

		return self::$repository;
	}
}
