<?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 LinkedArrayValues implements TabulatableColumnCallback
{
    public function __construct(
        private string $linkField,
        private string $labelField,
        private SimpleField $simpleField
    ) {}

    public function __invoke(HasTabulatorViewInterface $entity, string $field): array
    {
        $labels = ($this->simpleField)($entity, $this->labelField);
        $links = ($this->simpleField)($entity, $this->linkField);
        $values = [];

        foreach ($labels as $ix => $label) {
            $values[] = sprintf('<a href="%s">%s</a>', $links[$ix] ?? '', trim($label));
        }

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