<?php

namespace Plainwork\UiWordPress\Renderer;

use Plainwork\Http\Request;
use Plainwork\Ui\Component\Menu;
use Plainwork\Ui\Component\Link;
use Plainwork\Ui\Component\HtmlElement;

class AdminMenuRenderer
{
    public function __invoke(Menu $el, Request $request, callable $next)
    {
        $items = $el->getItems();
        foreach ($items as $item) {
            $isLink = false;
            if ($item instanceof Link) {
                $isLink = true;
            } elseif (($item instanceof HtmlElement) && ('a' === $item->getTag())) {
                $isLink = true;
            }

            if ($isLink) {
                $item->withAttr('class', 'page-title-action');
            }
        }

        return $next($el, $request);
    }
}
