<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;

class ShareTranslations
{
    public function handle(Request $request, Closure $next)
    {
        $locale = $request->segment(1);
        $translations = [];
        Log::info('locale:', [$locale]); // Log locale

        $files = glob(base_path("lang/{$locale}/*.php"));
        foreach ($files as $file) {
            $key = basename($file, '.php');
            $translations[$key] = require $file;
        }


        Inertia::share(['translations' => $translations, 'locale' => $locale]);

        return $next($request);
    }
}
