<?php
namespace App\Form\Input;

class Autocomplete_Data_Spotlight extends \Engine\Form\Input\Autocomplete_Data_GeneralAbstract
{
	/**
	 * (non-PHPdoc)
	 * @see \Engine\Form\Input\Autocomplete_Data_GeneralAbstract::getPageResultsBulk()
	 */
	protected function getPageResultsBulk() :array
	{
		$spotlightSearch = new \App\Spotlight\Search($this->searchString);

		$spotlightSearch->addSource(new \App\Spotlight\Source\Menu());
		$spotlightSearch->addSource(new \App\Spotlight\Source\MathOperations());
		$spotlightSearch->addSource(new \App\Spotlight\Source\CurrencyConvert());	// TODO: add "Currency rate " separat
		$spotlightSearch->addSource(new \App\Spotlight\Source\OptionsSimple());
		$spotlightSearch->addSource(new \App\Spotlight\Source\OptionsAutocompleteEntity());

		// prepare records for autocomplete fields
		$records = $spotlightSearch->getRecords($this->offset, $this->resultsCountPage) ; // returns array of max $resultsPerPage records (as objects) common object - Record[]

		$return = [];
		foreach ($records as $record) {
			$return[]= $this->getCleanItem(null, $record->label, $record->type, $record->tooltip, [
				'jsCallback' => $record->jsCallback,
				'classNames' => $record->classNames,
			]);
		}

		$this->resultsCountTotal = $spotlightSearch->getLastSearchTotalCount();

		return $return;
	}


	/**
	 * (non-PHPdoc)
	 * @see \Engine\Form\Input\Autocomplete_Data_GeneralAbstract::getPageResultsFormatted()
	 */
	protected function getPageResultsFormatted(Array $pageResults)
	{

		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();

		foreach ($pageResults as $id=>$row) {
			$pageResults[$id]['extra'] = $_LANG['RemoteData']['AutocompleteFieldData']['index']['spotlightOptions'][$row['extra']];
		}

		return $pageResults;
	}

}