<?php

namespace Uhura\Grossmarkt\ViewHelpers;

use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

/**
 * Class GenerateVerticalMapXBindViewHelper
 */
class GenerateVerticalMapXBindViewHelper extends AbstractViewHelper
{

    public function initializeArguments(): void
    {
        $this->registerArgument('array', 'array', 'Array with data', true);
        $this->registerArgument('positions', 'string', 'positions comma separated', true);
    }

	/**
	 * @return string
	 */
    public function render()
    {
		$array = $this->arguments['array'];
		$positions = explode(',', $this->arguments['positions']);


		$result = [];
		foreach ($array as $value) {
			if ($value['position']) {
				preg_match('/\d+/', $value['position'], $matches);
				$position = $matches[0];
				$value['position'] = $position;
			}
			if(in_array($value['position'], $positions)) {
				$result[] = $value;
			}
		}

		$val = null;
		if ($result) {
			$val = 'planPopoverTrigger(' . json_encode($result) . ')';
		}

		return $val;
    }
}
