<?php

namespace App\Http\Middleware;

use App\Models\Language;
use Closure;
use Illuminate\Http\Request;

class SetStoreLocaleFromSession
{
    public function handle(Request $request, Closure $next)
    {
        $locale = session('store_locale')
            ?: (Language::where('site_default', true)->value('a2_iso') ?: config('app.locale'));
        if ($locale) {
            app()->setLocale($locale);
        }

        return $next($request);
    }
}
