<?php
/**
 * The configuration of a field instance.
 *
 * @package Ealab\Deployer
 */

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

use EaLab\WP_Deployer\vendor\Ealab\Deployer\Tracking\ItemValueHandler\ScalarItemValueHandler;

/**
 * Holds the configuration of a field instance.
 */
class FieldConfig {
	/**
	 * The configuration of this field instance.
	 *
	 * It may contain the following keys:
	 * - value_handler (string): The full handler ID of the field. If not present, the scalar handler will be used.
	 * - processors (array): The processors config to apply on the value on export and import.
	 *   See ValueProcessorBase::$config for more information.
	 *
	 * @var array<string,mixed>
	 */
	private array $raw_config = array();

	/**
	 * Initializes the instance.
	 *
	 * @param array<string,mixed> $raw_config The configuration as array of the field.
	 *   See the raw_config property for more information.
	 */
	public function init( array $raw_config ) {
		$this->raw_config = $raw_config;
	}

	/**
	 * Gets the full handler ID of the field.
	 *
	 * If the "value_handler" key is not present in the raw config, the ID of the "scalar" handler will be used.
	 *
	 * @return string The full handler ID of the field.
	 */
	public function getFullValueHandlerId(): string {
		return $this->raw_config['value_handler'] ?? ScalarItemValueHandler::getHandlerBaseId();
	}

	/**
	 * Gets the processor config which are to append to the field instance holding this config.
	 *
	 * See ValueProcessorBase::$config for more information.
	 *
	 * @return array|null The processor config, or null if no processor is configured.
	 */
	public function getProcessorConfig(): ?array {
		return $this->raw_config['processors'] ?? array();
	}

	/**
	 * Gets the raw config of this field instance.
	 *
	 * @return array<string,mixed> The raw config of this field instance.
	 */
	public function getRawConfig(): array {
		return $this->raw_config;
	}

	/**
	 * Tells if this field instance should be excluded from the tracking.
	 *
	 * @return bool True if this field instance should be excluded from the tracking, false otherwise.
	 *   Defaults to false if the "exclude" key is not present in the raw config.
	 */
	public function isExcluded(): bool {
		return $this->raw_config['exclude'] ?? false;
	}
}
