<?php

namespace Apexl\Io\Display\Components;

use Apexl\Config\Exceptions\MissingConfigException;
use Apexl\Io\Component\Collections\ComponentCollection;
use Apexl\Io\Component\Collections\ComponentPropertyCollection;
use Apexl\Io\Component\Components\Component;
use Apexl\Io\Component\Factories\ComponentPropertyFactory;
use Apexl\Io\Exceptions\RouteNotFoundException;
use Apexl\Io\Services\HttpPaths;
use Apexl\Io\Services\RouteUrls;
use Arrayy\Type\StringCollection;

readonly class MessageList extends Component
{
    public function __construct(
        private RouteUrls $routeUrls,
        private HttpPaths $httpPaths,
        ComponentPropertyFactory $propertyFactory,
        ComponentPropertyCollection $props,
        ComponentCollection $components,
        StringCollection $classes,
        ?string $componentId = null
    ) {
        parent::__construct(
            $propertyFactory,
            $props,
            $components,
            $classes,
            $componentId
        );
    }

    public function setItems($messages): static
    {
        $this->setProperty('messages', $messages);

        return $this;
    }

    /**
     * @throws MissingConfigException
     * @throws RouteNotFoundException
     */
    public function addConfirmButton($route, $text, $class = ""): static
    {
        if ($url = $this->routeUrls->url($route)) {
            $this->setProperty(
                'confirmButtonUrl',
                $this->httpPaths->backendUrl($url)
            );
            $this->setProperty('confirmButtonText', $text);
            if ($class) $this->setProperty('confirmButtonClass', $class);
        }

        return $this;
    }


}
