<?php

namespace Medienreaktor\Energize\ViewHelpers;


use TYPO3\CMS\Core\Utility\GeneralUtility;

class GetCurrentPaginationPageViewHelper extends \TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper
{

    /**
     * @var bool
     */
    protected $escapeOutput = false;

    /**
     * Arguments Initialization
     */
    public function initializeArguments(): void {
        $this->registerArgument('as', 'string', 'The variable name in which the data should be saved', TRUE);
        $this->registerArgument('pluginKey', 'string', 'The plugin key of the template to check the pagination from', TRUE);
    }

    /**
     * Render method
     * @return string
     */
    public function render() {

        $as = $this->arguments['as'];
        $pluginKey = $this->arguments["pluginKey"];

        $return = 1;
        $pluginParams = $GLOBALS['TYPO3_REQUEST']->getQueryParams()[$pluginKey] ?? null;
        if (is_array($pluginParams) && array_key_exists("@widget_0", $pluginParams)) {
            $widgetParams = $pluginParams["@widget_0"];
            if (is_array($widgetParams) && array_key_exists("currentPage", $widgetParams)) {
                $return = $widgetParams["currentPage"];
            }
        }


        $this->templateVariableContainer->add($as, $return);
        $output = $this->renderChildren();
        $this->templateVariableContainer->remove($as);
        return $output;
    }

}