<?php

declare(strict_types=1);

namespace ProPresenter\Parser;

use Rv\Data\MacrosDocument\MacroCollection as MacroCollectionProto;

/**
 * Wraps a protobuf MacroCollection. A collection groups macros by UUID
 * reference; macro definitions themselves live at the document root.
 */
class MacroCollection
{
    public function __construct(
        private readonly MacroCollectionProto $collection,
    ) {
    }

    /**
     * Get the collection's UUID as a string (empty when unset).
     */
    public function getUuid(): string
    {
        return $this->collection->getUuid()?->getString() ?? '';
    }

    /**
     * Get the collection's display name (e.g. "Ablauf").
     */
    public function getName(): string
    {
        return $this->collection->getName();
    }

    /**
     * Get the UUIDs of macros referenced by this collection, in order.
     *
     * Items in the protobuf use a `oneof` ItemType — currently only
     * `macro_id` is defined. Items without a populated reference are skipped.
     *
     * @return string[]
     */
    public function getMacroUuids(): array
    {
        $uuids = [];
        foreach ($this->collection->getItems() as $item) {
            $macroId = $item->getMacroId();
            if ($macroId !== null) {
                $uuids[] = $macroId->getString();
            }
        }

        return $uuids;
    }

    /**
     * Get the underlying protobuf MacroCollection message.
     */
    public function getProto(): MacroCollectionProto
    {
        return $this->collection;
    }
}
