<?php

namespace Modules\Admin\App\Http\Middleware;

use Closure;
use Modules\Admin\App\Services\AdminTemplateService;
use Illuminate\Support\Facades\View;

class AdminTemplateMiddleware
{
    public function __construct()
    {
        //
    }

    public function handle($request, Closure $next)
    {
        // Verificar si estamos en el contexto de website
        if (!$request->is('admin/*') && !$request->is('admin')) {
            return $next($request);
        }

        // Compartir variables globalmente solo si es una solicitud HTML
        if (str_contains($request->header('Accept'), 'text/html')) {
            $adminVars = app(AdminTemplateService::class)->getAdminVars();

            View::share('_admin', $adminVars);
        }

        return $next($request);
    }
}
