<?php
/**
 * @var $breadcrumbs
 */

use Johncms\NavChain;

$nav_chain = $container->get(NavChain::class);
$breadcrumbs = $nav_chain->getAll();
$last_item = array_key_last($breadcrumbs);
?>
<?php if (! empty($breadcrumbs)): ?>
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb" vocab="https://schema.org/" typeof="BreadcrumbList">
            <?php foreach ($breadcrumbs as $key => $breadcrumb): ?>
                <?php
                $breadcrumbName = $this->e((string) ($breadcrumb['name'] ?? ''));
                $breadcrumbUrl = $this->e((string) ($breadcrumb['url'] ?? ''));
                ?>
                <li class="breadcrumb-item <?= ! empty($breadcrumb['active']) ? 'active' : '' ?>" property="itemListElement" typeof="ListItem">
                    <?php if (! empty($breadcrumb['url'])): ?>
                        <a <?php if ($last_item !== $key): ?>property="item" typeof="WebPage"<?php endif; ?> href="<?= $breadcrumbUrl ?>" title="<?= $breadcrumbName ?>">
                            <span<?php if ($last_item !== $key): ?> property="name"<?php endif; ?>><?= $breadcrumbName ?></span>
                        </a>
                        <?php if ($last_item === $key): ?><meta property="name" content="<?= $breadcrumbName ?>"><?php endif; ?>
                    <?php else: ?>
                        <span property="name"><?= $breadcrumbName ?></span>
                    <?php endif; ?>
                    <meta property="position" content="<?= ($key + 1) ?>">
                </li>
            <?php endforeach; ?>
        </ol>
    </nav>
<?php endif; ?>
