<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Navigation;

final class DesignerBreadcrumb
{
    public function __construct(
        private readonly string $breadcrumbId,
        private readonly array $items = [],
        private readonly string $style = 'simple'
    ) {}

    public static function create(string $breadcrumbId, array $items = [], string $style = 'simple'): self
    {
        return new self($breadcrumbId, $items, $style);
    }

    public static function fromPage(string $pageId, string $title, array $parents = []): self
    {
        $items = [['id' => 'home', 'label' => 'Home', 'target' => 'dashboard']];
        foreach ($parents as $parent) {
            if (is_array($parent)) {
                $items[] = $parent;
            }
        }
        $items[] = ['id' => $pageId, 'label' => $title, 'target' => $pageId, 'current' => true];

        return new self($pageId . '_breadcrumb', $items, 'simple');
    }

    public function toComponent(): array
    {
        return [
            'id' => $this->breadcrumbId,
            'name' => $this->breadcrumbId,
            'type' => 'breadcrumb',
            'label' => 'Breadcrumb',
            'props' => $this->toArray(),
            'children' => [],
            'meta' => ['designerRuntime' => 'breadcrumb', 'version' => '1.0.0'],
        ];
    }

    public function toArray(): array
    {
        return [
            'breadcrumbId' => $this->breadcrumbId,
            'id' => $this->breadcrumbId,
            'items' => $this->items,
            'style' => $this->style,
            'itemCount' => count($this->items),
        ];
    }
}
