<?php
/**
 * Helps retrieving item value handlers.
 *
 * @package Ealab\Deployer
 */

namespace EaLab\WP_Deployer\vendor\Ealab\Deployer\Tracking\ItemValueHandler;

use EaLab\WP_Deployer\vendor\Dakwamine\Component\ComponentBasedObject;

/**
 * Helps retrieving item value handlers.
 */
class ItemValueHandlerManager extends ComponentBasedObject {

	/**
	 * The handler classes, keyed by base handler ID.
	 *
	 * @var array<class-string<ItemValueHandlerBase>>
	 */
	private array $handler_classes = array();

	/**
	 * Whether the handler classes have been collected.
	 *
	 * @var bool
	 */
	private bool $has_collected_classes = false;

	/**
	 * Gets the handler class name for the given handler ID.
	 *
	 * @param string $handler_id The handler id. It can be a full handler ID, or a handler base ID.
	 *
	 * @return class-string<ItemValueHandlerBase>|null The handler class name, or null if the handler does not exist.
	 */
	public function getHandlerClassNameById( string $handler_id ): ?string {
		$handler_base_id = explode( ':', $handler_id, 2 )[0];

		$handler_class_names = $this->getHandlerClassNames();

		if ( empty( $handler_class_names[ $handler_base_id ] ) ) {
			return null;
		}

		return $handler_class_names[ $handler_base_id ];
	}

	/**
	 * Gets the handler class names.
	 *
	 * @return array<class-string<ItemValueHandlerBase>> The handler class names, keyed by handler base id.
	 */
	public function getHandlerClassNames(): array {
		// Todo: faire les classes "Serialized" et "SerializedReference", entre autres?
		if ( ! $this->has_collected_classes ) {
			$this->handler_classes       = apply_filters( 'deploy_sync_content__item_value_handler_manager__get_class_names', array() );
			$this->has_collected_classes = true;
		}

		return $this->handler_classes;
	}

}
