<?php

declare(strict_types=1);

namespace App\Support\Ui\EnterpriseDetail;

final readonly class SupportingCardData
{
    /**
     * @param  list<array<string, mixed>>  $items
     * @param  array{title: string, description?: ?string, icon?: ?string}|null  $emptyState
     * @param  array<string, mixed>  $viewData
     */
    public function __construct(
        public string $kind,
        public string $title,
        public array $items = [],
        public bool $visible = true,
        public ?PageActionData $action = null,
        public ?string $description = null,
        public ?string $view = null,
        public array $viewData = [],
        public ?array $emptyState = null,
    ) {}

    public function shouldRender(): bool
    {
        if (! $this->visible || trim($this->title) === '') {
            return false;
        }

        return $this->items !== []
            || $this->view !== null
            || $this->emptyState !== null;
    }

    /**
     * @return array{
     *     kind: string,
     *     title: string,
     *     items: list<array<string, mixed>>,
     *     visible: bool,
     *     action: array{label: string, placement: string, url: ?string, actionName: ?string, destructive: bool, requiresConfirmation: bool, visible: bool, icon: ?string, openInNewTab: bool}|null,
     *     description: ?string,
     *     view: ?string,
     *     viewData: array<string, mixed>,
     *     emptyState: array{title: string, description?: ?string, icon?: ?string}|null
     * }
     */
    public function toArray(): array
    {
        return [
            'kind' => $this->kind,
            'title' => $this->title,
            'items' => array_values($this->items),
            'visible' => $this->visible,
            'action' => $this->action?->isRenderable() === true ? $this->action->toArray() : null,
            'description' => $this->description,
            'view' => $this->view,
            'viewData' => $this->viewData,
            'emptyState' => $this->emptyState,
        ];
    }
}
