<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\View\Middleware\ShareErrorsFromSession;

class ConditionalShareErrorsFromSession extends ShareErrorsFromSession
{
    public function handle($request, Closure $next): mixed
    {
        if (! $request instanceof Request) {
            return parent::handle($request, $next);
        }

        if ($request->attributes->get('skinbase.session_skipped') === true || ! $request->hasSession()) {
            return $next($request);
        }

        return parent::handle($request, $next);
    }
}