<?php

namespace apexl\Io\modules\display\components;

use apexl\Io\modules\component\Collections\ComponentCollection;
use apexl\Io\modules\component\Collections\ComponentPropertyCollection;
use apexl\Io\modules\component\Component\Component;
use apexl\Io\modules\component\Factories\ComponentPropertyFactory;
use Arrayy\Type\StringCollection;

/**
 * @deprecated Builder-pattern component incompatible with the readonly Component contract — mutating methods (addMenuItems()) fatal at runtime on write-once properties. Superseded by tabulator-based components: io-tabulator (Tabulator / EntityTabulator / AggregateTabulator); user/role admin screens via apexl/io-user-tabulator. Do not use in new code; removal planned once the legacy io-user userDisplay routes are gone.
 */
readonly class DropMenu extends Component
{
    // setName() is part of the io-component 3.1.7 compatibility shim (slated
    // for reversal) and doesn't exist on the strict base. Both the strict and
    // shimmed Component constructors resolve the component name from a `NAME`
    // class constant when present, so declare it instead of calling setName().
    public const string NAME = 'DropMenu';

    // Property default values are illegal on readonly classes (pre-existing
    // parse error) — initialised in the constructor instead. NOTE:
    // addMenuItems() reassigns this post-construction, which is incompatible
    // with readonly write-once semantics — flagged for design review; only
    // the parse error is fixed here.
    protected array $items;

    public function __construct(
        ComponentPropertyFactory $propertyFactory,
        ComponentPropertyCollection $props,
        ComponentCollection $components,
        StringCollection $classes,
        ?string $componentId = null
    ) {
        parent::__construct($propertyFactory, $props, $components, $classes, $componentId);
        $this->items = [];
    }

    public function addMenuItems($items, $add = true)
    {
        if ($add && count($this->items) > 0) {
            $this->items = array_merge($this->items, $items);
        } else {
            $this->items = $items;
        }

        $this->setProperty('items', $this->items);
        $this->addClass('dropmenu-items-' . count($this->items));
        return $this;
    }

}
