<?php

namespace App\Form\Input;

class Cloned_KeyValueTextSelector extends \Engine\Form\Input\ElementAbstract
{
	protected $class = "clonedKeyValueTextSelector";

	protected $selectValues = [];

	protected $showDefaultOption=null;

	protected $defaultOptionText = '-';

	public function __construct($label=null, $name=null, $value=null)
	{
		parent::__construct($label, $name, $value);

		$this->settings['minItems'] = 1;

		$this->settings['operations'] = [];
		$this->settings['operations']['add'] = true;
		$this->settings['operations']['remove'] = true;
		$this->settings['operations']['default'] = false;
		$this->settings['forceSelect'] = true;
		$this->settings['showCounter'] = false;
	}

	public function setSelectValues(array $possibleSelectorValues)
	{
		$this->selectValues = (array) $possibleSelectorValues;

	}

	/**
	 * Sets the showDetaultValue attribute.
	 * @param Boolean $show
	 * @return self
	 */
	public function showDefaultOption($show=true)
	{
		$this->showDefaultOption=(bool)$show;
		return $this;
	}

	/**
	 * Sets the default option text.
	 * @param String $text
	 * @return \Engine\Form\Input\Select
	 */
	public function setDefaultOptionText($text)
	{
		$this->defaultOptionText = $text;
		return $this;
	}

	/**
	 * Sets labels for each input element
	 * @param array $labels
	 */
	public function setElementLabels(array $labels)
	{
		foreach($labels as $name => $labelStr) {
			$this->settings['labels'][$name] = $labelStr;
		}
	}

	/**
	 * description
	 */
	protected function getTemplateParams($templateClass = null, $parentElement = null)
	{
		$params = parent::getTemplateParams($templateClass, $parentElement);

		$params['selectValues'] = $this->selectValues;

		// clean the list of possible values (if manually set to true/false)
		// if we do NOT want default option - eliminate if (if there is one)
		if ($this->showDefaultOption===false) {
			unset($params['selectValues']['']);
		}
		// if we DO want a default option, add it if it does not already exist
		if ($this->showDefaultOption===true) {
			if (!$params['selectValues']['']) {
				$params['selectValues']= [''=>$this->defaultOptionText] + $params['selectValues'];
			}
		}

		return $params;
	}
}
