<?php

namespace Internetgalerie\IgDatapoolFe\ViewHelpers\Form;

use TYPO3\CMS\Core\Page\PageRenderer;

/**
 * Renders an AJAX ObjectStorage of FileReferences Field.
 */
class FileReferencesViewHelper extends AbstractFileReferenceViewHelper
{
    // max Files to Upload
    protected $maxUploads=10;

    // remove if v12 is dropped
    protected PageRenderer $pageRenderer;

    public function injectPageRenderer(PageRenderer $pageRenderer): void
    {
        $this->pageRenderer = $pageRenderer;
    }

    /**
     * Initialize the arguments.
     *
     * @return void
     * @api
     */
    public function initializeArguments(): void
    {
        parent::initializeArguments();
        $this->registerArgument('maxUploads', 'int', 'Max number of simultaneous uploads.', false, 10);
    }

    /**
     * Renders the upload field with possible resource pointer
     *
     * @return string
     * @api
     */
    public function render(): string
    {
        $this->pageRenderer->loadJavaScriptModule('@dp/elements/upload.js');
        $this->formUtility->addJsFooter();
        $this->prepareOptions();

        $this->options['multiple']=true;
        $this->options['multipleSelect']=true;

        if ($this->dragAndDrop) {
            $this->cssClasses[]='dp-dropzone';
        }

        $value = $this->getUploadedResource();


        return $this->renderHtml($value);
    }
}
