<?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 Sum implements TabulatableColumnCallback
{
    public function __construct(private array $fields) {}

    public function __invoke(HasTabulatorViewInterface $entity, string $field): int|float
    {
        return array_reduce(
            $this->fields,
            fn(int|float $carry, string $field): int|float => $carry + ($entity->{$field} ?? 0),
            0
        );
    }
}
