<?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;
use apexl\Io\modules\tabulator\Services\TabulatorFieldRenamer;

final readonly class LinkField implements TabulatableColumnCallback
{
    private string $link;
    private string $label;

    public function __construct(
        string $link,
        string $label,
        private bool $newWindow,
        private SimpleField $simpleField,
        TabulatorFieldRenamer $fieldRenamer,
    ) {
        $this->label = $fieldRenamer->rename($label);
        $this->link = $fieldRenamer->rename($link);
    }

    public function __invoke(HasTabulatorViewInterface $entity, string $field): array
    {
        return [
            'link' => ($this->simpleField)($entity, $this->link),
            'label' => ($this->simpleField)($entity, $this->label),
            'newWindow' => $this->newWindow,
        ];
    }
}
