<?php
/**
 * Hyvä Themes - https://hyva.io
 * Copyright © Hyvä Themes 2020-present. All rights reserved.
 * This product is licensed per Magento install
 * See https://hyva.io/license
 */

declare(strict_types=1);

use Magento\Theme\Block\Html\Breadcrumbs;
use Magento\Framework\Escaper;

/** @var Escaper $escaper */
/** @var Breadcrumbs $block */
/** @var array[] $crumbs */

// Get base URL for home link
$baseUrl = $block->getUrl('');
$storeUrl = rtrim($baseUrl, '/');
?>
<?php if ($crumbs && is_array($crumbs)): ?>
    <nav class="breadcrumbs bg-pagination shadow-sm hidden lg:block" aria-label="Breadcrumb">
        <div class="px-3 2xl:container 3xl:max-w-screen-2xl py-1">
            <ol class="items list-reset py-2 rounded flex flex-wrap text-grey text-sm">
                <?php foreach ($crumbs as $crumbName => $crumbInfo): ?>
                    <li class="item flex items-center text-black font-semibold <?= $escaper->escapeHtmlAttr($crumbName) ?>">
                        <?php if (!$crumbInfo['first']): ?>
                            <span aria-hidden="true" class="separator text-black px-2 text-sm font-inter">
                                <svg xmlns="http://www.w3.org/2000/svg" width="6" height="7" viewBox="0 0 6 7" fill="none">
                                  <path d="M0 7V0L5.5 3.5L0 7Z" fill="#1C1C1C"/>
                                </svg>
                            </span>
                        <?php endif; ?>
                        <?php if ($crumbInfo['link']): ?>
                            <a
                                href="<?= $escaper->escapeUrl($crumbInfo['link']) ?>"
                                <?php if ($crumbInfo['title']): ?>
                                    aria-label="<?= $escaper->escapeHtmlAttr($crumbInfo['title']) ?>"
                                <?php endif; ?>
                            ><?= $escaper->escapeHtml($crumbInfo['label']) ?></a>
                        <?php elseif ($crumbInfo['last']): ?>
                            <span
                                class="text-black text-sm font-inter"
                                aria-current="page"
                            ><?= $escaper->escapeHtml($crumbInfo['label']) ?></span>
                        <?php else: ?>
                            <?= $escaper->escapeHtml($crumbInfo['label']) ?>
                        <?php endif; ?>
                    </li>
                <?php endforeach; ?>
            </ol>
        </div>
    </nav>

    <?php if (count($crumbs) > 0):
        $breadcrumbItems = [];
        $position = 1;

        // Add home as first item if not already included
        if (!isset($crumbs['home'])) {
            $breadcrumbItems[] = [
                '@type' => 'ListItem',
                'position' => $position++,
                'name' => 'Home',
                'item' => $storeUrl . '/'
            ];
        }

        foreach ($crumbs as $crumbName => $crumbInfo) {
            $url = $crumbInfo['link'] ?: $block->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
            $breadcrumbItems[] = [
                '@type' => 'ListItem',
                'position' => $position++,
                'name' => $crumbInfo['label'],
                'item' => $url
            ];
        }

        $breadcrumbSchema = [
            '@context' => 'http://schema.org',
            '@type' => 'BreadcrumbList',
            'itemListElement' => $breadcrumbItems
        ];
    ?>
        <script type="application/ld+json">
        <?= json_encode($breadcrumbSchema) ?>
        </script>
    <?php endif; ?>
<?php endif; ?>
