<?php

namespace Lightscale\LaralightTables\Columns;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\HtmlString;
use Illuminate\View\ComponentAttributeBag;

use Closure;

abstract class ElementColumn extends Column
{

    protected string $element;
    private ?Closure $elemAttributesFn = null;

    public function attributes(callable $fn) : static
    {
        $this->elemAttributesFn = Closure::fromCallable($fn);
        return $this;
    }

    protected function getElemAttributes(Model $row) : array
    {
        return $this->elemAttributesFn?->call($this, $row) ?? [];
    }

    protected function getContent(Model $row): string
    {
        $content = parent::getContent($row);
        $attributes = new ComponentAttributeBag($this->getElemAttributes($row));
        $attributes = $attributes->toHtml();

        return new HtmlString(
            "<{$this->element} {$attributes}>{$content}</{$this->element}>"
        );
    }

}