<?php

namespace Plainwork\Ui\Renderer;

use Plainwork\Http\Request;
use Plainwork\Ui\Ui;
use Plainwork\Ui\Html;
use Plainwork\Ui\Component\HtmlElement;
use Plainwork\Ui\Component\Breadcrumbs;
use Plainwork\Ui\Component\Menu;
use Plainwork\Ui\Component\Link;
use Plainwork\Ui\Component\Icon;

final class BreadcrumbsRenderer
{
    public $ui = Ui::class;

    public function __invoke(Breadcrumbs $el, Request $request)
    {
        $items = $el->getItems();
        if (!$items) {
            return;
        }

        $html = new Html();

        $ret = $html->menu();
        $i = -1;
        foreach ($items as $item) {
            ++$i;
            if ($i > 0) {
                $ret->add($html->icon('/'));
            }
            $ret->add($item);
        }

        return $ret;


        $items = array_map(fn($e) => $this->ui->render($e, $request), $items);
        $items = array_filter($items, fn($e) => (null !== $e));
        $items = array_filter($items, fn($e) => strlen($e) ? true : false);
        if (!$items) {
            return;
        }

        $ret = new Menu();
        $i = -1;
        foreach ($items as $item) {
            ++$i;
            if ($i > 0) {
                $ret->add(new Icon('/'));
            }
            $ret->add($item);
        }

        // $ret = new HtmlElement('small', $ret);

        return $ret;
    }
}
