<?php
declare(strict_types=1);

namespace KbMarkdownImporter\Frontend;

use KbMarkdownImporter\Plugin;

final class BreadcrumbBuilder
{
    public function build(array $parts): string
    {
        $base = trim((string) Plugin::settings()['docs_base_slug'], '/');
        $items = [
            sprintf('<a href="%s">%s</a>', esc_url(home_url('/' . $base . '/')), esc_html__('Dokumentation', 'kb-markdown-importer')),
        ];
        $path = $base;

        foreach ($parts as $label => $slug) {
            if ('' === (string) $slug) {
                $items[] = esc_html((string) $label);
                continue;
            }

            $path .= '/' . trim((string) $slug, '/');
            $items[] = sprintf('<a href="%s">%s</a>', esc_url(home_url('/' . $path . '/')), esc_html((string) $label));
        }

        return '<nav class="kb-breadcrumbs" aria-label="Breadcrumb">' . implode('<span>/</span>', $items) . '</nav>';
    }
}
