<?php

declare(strict_types=1);

namespace App\Application\Admin\ValueObjects;

final readonly class NavigationMenu
{
    /** @param array<NavigationSection> $sections */
    public function __construct(
        public array $sections
    ) {
    }

    public static function fromArray(array $data): self
    {
        $sections = [];
        foreach ($data as $sectionData) {
            $sections[] = NavigationSection::fromArray($sectionData);
        }

        return new self($sections);
    }

    public function toArray(): array
    {
        return array_map(
            fn (NavigationSection $section) => $section->toArray(),
            $this->sections
        );
    }

    public function addSection(NavigationSection $section): self
    {
        return new self([...$this->sections, $section]);
    }

    public function findSectionByName(string $name): ?NavigationSection
    {
        foreach ($this->sections as $section) {
            if ($section->name === $name) {
                return $section;
            }
        }

        return null;
    }
}
