<?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 Money\Formatter\IntlMoneyFormatter;
use Money\Money;

final readonly class FormatMoney implements TabulatableColumnCallback
{
    public function __construct(private IntlMoneyFormatter $formatter) {}

    public function __invoke(HasTabulatorViewInterface $entity, string $field): string
    {
        if ($entity->{$field} instanceof Money) {
            return $this->formatter->format($entity->{$field});
        }

        return '';
    }
}

