<?php

declare(strict_types=1);

namespace FleekDash\Admin;

/**
 * Decides when the Vite/React admin shell should load vs PHP-only classic screens.
 */
final class ReactAdminShellResolver
{
    /**
     * Admin `page=` slugs under FleekDash that must NOT mount the SPA (PHP-only tools).
     *
     * @var list<string>
     */
    private const EXCLUDED_PAGE_SUFFIXES = [
        'fleekdash-data-sources',
    ];

    public static function shouldLoad(string $hook): bool
    {
        $page = isset($_GET['page']) ? (string) $_GET['page'] : '';

        if (self::isExcludedScreen($hook, $page)) {
            return false;
        }

        return ($page !== '' && strpos($page, 'fleekdash') === 0)
            || strpos($hook, 'toplevel_page_fleekdash') === 0
            || strpos($hook, 'fleekdash_page_fleekdash') === 0;
    }

    /**
     * PHP-only admin screens: never mount the SPA; match by `page` and by hook (e.g. …_page_fleekdash-data-sources).
     */
    private static function isExcludedScreen(string $hook, string $page): bool
    {
        foreach (self::EXCLUDED_PAGE_SUFFIXES as $suffix) {
            if ($page === $suffix) {
                return true;
            }
            if ($hook !== '' && str_contains($hook, $suffix)) {
                return true;
            }
        }

        return false;
    }
}
