<?php

namespace Plainwork\Ui\Renderer\Form;

use Plainwork\Http\Request;
use Plainwork\Http\Server;
use Plainwork\Ui\Component\Form\AbstractInput;
use Plainwork\Ui\Component\BlockList;
use Plainwork\Ui\Component\HtmlElement;

class InputRenderer
{
    public $server = Server::class;

    public function __invoke(AbstractInput $input, Request $request, callable $next)
    {
        $prefix = $this->server->getPrefix();

        $name = $input->getName();
        $htmlName = strlen($prefix) ? $prefix . $name : $name;
        $input = $input->withAttr('name', $htmlName);

        if ($input->isRequired()) {
            $input->withAttr('required', true);
        }
        if ($input->isDisabled()) {
            $input->withAttr('disabled', true);
        }

        $errors = $input->getErrors();
        if ($errors) {
            $input->withAttr('aria-invalid', true);
        }
        $helps = $input->getHelps();
        // if ($errors or $helps) {
            // $input->withAttr('style', ['border' => 'red 2px solid']);
        // }
        return $next($input, $request);
    }
}
