<?php
/**
 * Screen label formatter base.
 *
 * @package Ealab\Deployer
 */

namespace Ealab\Deployer\Tracking\ScreenLabelFormatter;

use Dakwamine\Component\ComponentBasedObject;
use Dakwamine\Component\RootDependencyDefinition;
use Ealab\Deployer\Exception\LabelFormatterException;
use Ealab\Deployer\Exception\WpDeployException;
use Ealab\Deployer\Tracking\ScreenLabelFormatterManager;
use Ealab\Deployer\Tracking\TrackableItem;
use Ealab\Deployer\Tracking\TrackingManager;
use Ealab\Deployer\Utility\FileManager;

/**
 * Screen label formatter base.
 *
 * Used to format screen labels of trackable/deployment item for display in UIs.
 */
class ScreenLabelFormatterBase extends ComponentBasedObject {
	/**
	 * The file manager.
	 *
	 * @var FileManager|null
	 */
	protected ?FileManager $file_manager = null;

	/**
	 * The screen label formatter manager.
	 *
	 * @var ScreenLabelFormatterManager|null
	 */
	protected ?ScreenLabelFormatterManager $screen_label_formatter_manager = null;

	/**
	 * The tracking manager.
	 *
	 * @var TrackingManager|null
	 */
	protected ?TrackingManager $tracking_manager = null;

	/**
	 * Formats using a file deployment item.
	 *
	 * @param string              $bundle_subdirectory The bundle subdirectory containing the file item.
	 * @param array               $file_item The file deployment item.
	 * @param array<string,mixed> $config The formatting config.
	 *
	 * @return string The label.
	 *
	 * @throws LabelFormatterException|WpDeployException If the label cannot be formatted.
	 */
	public function formatFromFileItem( string $bundle_subdirectory, array $file_item, ?array $config = null ): string {
		// The default implementation is to return the item's UUID.
		return $file_item['uuid'] ?? '';
	}

	/**
	 * Formats the screen label.
	 *
	 * @param TrackableItem            $item The trackable item that holds this screen label formatter.
	 * @param array<string,mixed>|null $config The formatting config.
	 *
	 * @return string The formatted screen label.
	 *
	 * @throws LabelFormatterException|WpDeployException If the screen label cannot be formatted.
	 */
	public function formatFromTrackableItem( TrackableItem $item, ?array $config = null ): string {
		// The default implementation is to return the item's UUID.
		return $item->getUuid();
	}

	/**
	 * {@inheritDoc}
	 */
	public function getDependencyDefinitions(): array {
		$definitions   = parent::getDependencyDefinitions();
		$definitions[] = new RootDependencyDefinition( FileManager::class, $this->file_manager );
		$definitions[] = new RootDependencyDefinition( ScreenLabelFormatterManager::class, $this->screen_label_formatter_manager );
		$definitions[] = new RootDependencyDefinition( TrackingManager::class, $this->tracking_manager );

		return $definitions;
	}

	/**
	 * The form build for this formatter, if any.
	 *
	 * @return array|null The form build, null if not needed.
	 */
	public static function getFormBuild(): ?array {
		return null;
	}

	/**
	 * Gets the ID of this screen label formatter.
	 *
	 * @return string The ID of this screen label formatter.
	 */
	public static function getId(): string {
		return 'base';
	}

	/**
	 * Gets the label of this screen label formatter.
	 *
	 * @return string
	 */
	public static function getLabel(): string {
		return _dpgettext( 'deployer-core', 'screen label formatter', 'Default' );
	}
}
