<?php

namespace Eminiarts\Aura\Pipeline;

use Closure;

class MapFields implements Pipe
{
    public function handle($fields, Closure $next)
    {

        // $backtrace = debug_backtrace();

        // // $latestTrace = $backtrace[0];

        // $backtraces = [];
        // foreach ($backtrace as $index => $trace) {
        //     if ($index >= 8) {
        //         break;
        //     }
        //     if (isset($trace['file']) && isset($trace['line']) && isset($trace['function'])) {
        //         $backtraces[] = $trace['file'] . ':' . $trace['line'] . ' - ' . $trace['function'];
        //     }
        // }

        // ray($backtraces);
        // ray('-----');
        //ray()->count();

        return $next($fields->map(function ($item) {
            $item['field'] = app($item['type'])->field($item);
            $item['field_type'] = app($item['type'])->type;

            return $item;
        }));
    }
}
