<?php

declare(strict_types=1);

namespace Builder\Core\Foundation\Dashboard\Widgets;

use Builder\Core\Foundation\Dashboard\Enums\WidgetWidth;
use Closure;

/**
 * A text/HTML content widget.
 *
 * Accepts a static string or a Closure that returns the content at runtime.
 *
 * Example:
 *   new TextWidget('welcome', 'Welcome', '<p>Hello admin!</p>')
 */
class TextWidget extends Widget
{
    protected WidgetWidth $width = WidgetWidth::Half;

    /** @param string|Closure():string $content */
    public function __construct(
        private readonly string $key,
        private readonly string $label,
        private readonly string|Closure $content,
    ) {}

    public function getKey(): string
    {
        return $this->key;
    }

    public function getLabel(): string
    {
        return $this->label;
    }

    public function getComponent(): string
    {
        return 'TextWidget';
    }

    /** @return array<string, mixed> */
    public function getData(): array
    {
        $content = is_callable($this->content) ? ($this->content)() : $this->content;

        return ['content' => $content];
    }
}
