<?php
namespace Hecate\Controls\Sections\TabSection;

use Hecate\Components\ComponentBase;
use Hecate\Controls\Components\ControlsRepository;
use Hecate\Lodash;
use Hecate\Controls\Components\BaseControl;

/**
 * The Accordion is places inside a Tab(Content/Style) and it contains controls.
 */
abstract class SectionAccordion {

	/**
	 * @var ComponentBase
	 */
	protected $component;


	/**
	 * @param ComponentBase $component
	 * @param string $tab
	 */
	public function __construct( $component ) {
		$this->component = $component;
	}

	/**
	 * This will contain the controls of the section. It will get overwritten by the descendants of this class.
	 * The descendants will add associative arrays with minimal information like settingId, settingType, label. And the
	 * getControls function will generate the BaseControl objects.
	 *
	 * @return string[]
	 */
	abstract public function getControlRules();


	/**
	 * Created the control list by merging the user defined data like setingId, settingType with computed data like
	 * sectionTab, sectionId, defaultValue etc... . The computed data don't need to be explicitly defined, so we determine them
	 * dynamically to reduce the control setting size. The controls are grouped in tabs and accordions to increase modularity.
	 * It's easier to manage the control when they are split up.
	 *
	 * @return BaseControl[]
	 */
	public function getControls() {
		$controlsList   = $this->getControlRules();

		$controlClasses = ControlsRepository::getControlClasses();
		$result         = array_map(
			function( $controlData ) use ( $controlClasses ) {
				$type         = Lodash::get( $controlData, 'type' );
				$controlClass = Lodash::get( $controlClasses, $type, ControlsRepository::$defaultControl );
				return new $controlClass( $controlData, $this->component );
			},
			$controlsList
		);

		return $result;
	}
}
