<?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 (addData()) 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 DisplayTable extends Component
{
    // Untyped properties are illegal on readonly classes (pre-existing parse
    // error). NOTE: addData() appends to this post-construction, which is
    // incompatible with readonly write-once semantics — flagged for design
    // review; only the parse error is fixed here.
    protected array $displayData;

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

    public function addTitle($title)
    {
        $this->setProperty('title', $title);
        return $this;
    }

    public function addData($title, $data = null, $component = null)
    {
        $display = [];
        $display['title'] = $title;
        if ($data) {
            $display['content'] = $data;
        }
        if ($component) {
            $display['component'] = (string) $component;
        }
        $this->displayData[] = (object) $display;
        $this->setProperty('data', $this->displayData);
        return $this;
    }
}
