<?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;

readonly class ArrayValues implements TabulatableColumnCallback
{
    public function __construct(private SimpleField $simpleField, private array $fields = []) {}

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

        foreach ($this->fields as $field) {
            $values[] = ($this->simpleField)($entity, $field);
        }

        return $this->flatten($values);
    }

    private function flatten(array $array): array
    {
        $return = [];
        array_walk_recursive($array, function ($a) use (&$return) {
            $return[] = $a;
        });

        return $return;
    }

}
