<?php

namespace Plainwork\Ui\Renderer\Form;

use Plainwork\Http\Request;
use Plainwork\Ui\Component\HtmlElement;
use Plainwork\Ui\Component\Form\Form;

final class FormRenderer
{
    private static $htmlInputId = 1;

    public function __invoke(Form $el, Request $request, callable $next)
    {
        static $formId = 0;
        ++$formId;

        $ret = (new HtmlElement('form', $el->getContent()))
            ->withAttr('method', 'post')
            ->withAttr('data-pw-id', 'form' . $formId)
            ;

        if ($el->isWithUpload()) {
            $ret->withAttr('enctype', 'multipart/form-data');
        }

        $attr = $el->getAttr();
        foreach ($attr as $k => $v) {
            $ret->withAttr($k, $v);
        }

        return $ret;
    }
}
