<?php

declare(strict_types=1);

namespace apexl\Io\modules\tabulator\Tabulatable\ColumnCallback;

use apexl\Io\modules\tabulator\Interfaces\HasTabulatorViewInterface;
use apexl\Io\modules\tabulator\Interfaces\TabulatableColumnCallback;

final readonly class Created implements TabulatableColumnCallback
{
    public function __construct(
        private SimpleField $simpleField,
        private ?string $dateField = 'created',
        private ?string $contactField = 'creator.name',
    ) {}

    public function __invoke(HasTabulatorViewInterface $entity, string $field): array
    {
        $values = [];

        if ($this->contactField) {
            $values[] = ($this->simpleField)($entity, $this->contactField);
        }


        if ($this->dateField) {
            $values[] = ($this->simpleField)($entity, $this->dateField);
        }

        return array_values(array_filter($values));
    }
}
