<?php

namespace AutoAdmin\Bundle\Content\Frontend;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class FrontendUrlChecker
{
    public function __construct(
        protected RequestStack $requestStack,
    ) {
    }

    /**
     * We want to keep front urls, so ignore:
     * - all sub urls of "/admin/"
     * - "_profiler" type urls (starting by "_")
     * - request without route
     *
     * @param Request|null $request
     * @return bool
     */
    public function isFrontUrl(Request $request = null): bool
    {
        if ($request === null) {
            $request = $this->requestStack->getMainRequest();
        }

        $route = $request->get('_route');
        $path = parse_url($request->getRequestUri(), PHP_URL_PATH);

        if (
            $route === null
            || str_starts_with($route, '_')
            || str_starts_with($route, 'aa_admin_')
            || str_starts_with($path, '/admin/')
        ) {
            $state = false;
        } else {
            $state = true;
        }

        return $state;
    }
}
