<?php

namespace App\Http\Middleware;

use App\Models\Language;
use Closure;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\View;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Setting\Services\SettingService;

class AdminConfig
{
    public function __construct(
        private SettingService $settingService,
    ){}

    /**
     * Handle an incoming request.
     * Redirect user to admin login page if he has not the previliage to access the admin panel
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        View::composer('*', function($view) {

            $target = explode('.',Route::currentRouteName());
            $video = $this->settingService->getItemByKey('intro_'.$target[0]);
            $view->with('video', $video);
            $languages = Language::cursor();
            $view->with('languages', $languages);

            $view->with('lang', LaravelLocalization::getCurrentLocale());

        });

        return $next($request);
    }
}
