<?php
/**
 * Provides tracker configuration from a repository.
 *
 * @package Ealab\Deployer
 */

namespace Ealab\Deployer\Tracking\TrackerConfig;

use Ealab\Deployer\Tracking\Config\ConfigProviderBase;
use Ealab\Deployer\Utility\DeployerConfigRepository\AvailableTrackersConfigFilter;
use Ealab\Deployer\Utility\DeployerConfigRepository\DeployerConfig;
use Ealab\Deployer\Utility\DeployerConfigRepository\InstalledPluginsConfigFilter;

/**
 * Provides tracker configuration from a repository.
 */
class RepositoryTrackerConfigProvider extends ConfigProviderBase {
	/**
	 * {@inheritDoc}
	 */
	public function collect(): ?array {
		$tracker_config = DeployerConfig::getConfig(
			'tracker',
			array(
				AvailableTrackersConfigFilter::class,
				array( InstalledPluginsConfigFilter::class ),
			),
		);

		// Finally extract the real config schema, discarding the metadata.
		$config = array_map(
			function ( $config ) {
				// The array is supposed to be indexed by tracker ID.
				// So we can take the first (and only) item.
				$config = reset( $config );

				return json_decode( $config['schema'], true );
			},
			$tracker_config
		);

		$this->config = $config ?? null;

		return $this->config;
	}
}
