<?php

namespace Planaday\Utility\DataFilters\Validators;

use Planaday\Utility\DataFilters\AdvancedValidator;

class Needs implements Validator {
	protected AdvancedValidator $validations;
	protected IsNeeded $isNeeded;

	/**
	 * @param AdvancedValidator $requirement
	 * @param mixed $checkData
	 * @param bool|null $startCheckingAt if null, will always run. if false, will run if data !== null. if true, will run if data === true.
	 */
	public function __construct(
		AdvancedValidator $requirement,
		protected mixed $checkData,
		protected ?bool $startCheckingAt = null
	) {
		$this->isNeeded = new IsNeeded( $this );
		$requirement->addValidation( $this->isNeeded );
		$this->validations = new AdvancedValidator($requirement->getData(), $requirement->getName());
	}

	public function validate( mixed $data ): array|bool {
		if ( $this->startCheckingAt === false && $this->checkData === null ) return true;
		if ( $this->startCheckingAt && $this->checkData !== true ) return true;

		return $this->validations->validate();
	}

	public function getName(): string {
		return "needs";
	}

	/**
	 * @return AdvancedValidator
	 */
	public function getValidations(): AdvancedValidator {
		return $this->validations;
	}
}