<?php
declare(strict_types=1);

/** Resolve o arquivo de CSS/JS compartilhado por páginas que reutilizam a mesma view. */
function panel_page_asset_key(string $page): string
{
    $page = strtolower(trim($page));
    $aliases = [
        'revenda_clientes' => 'users',
        'revenda_criar_cliente' => 'users_create',
        'revendas_criar' => 'revendas',
        'rom' => 'emuladores',
    ];
    $resolved = $aliases[$page] ?? $page;
    return preg_match('/^[a-z0-9_-]+$/', $resolved) ? $resolved : 'dashboard';
}

function panel_page_asset(string $type, string $page, string $baseUrl = ''): ?array
{
    if (!in_array($type, ['css', 'js'], true)) {
        return null;
    }
    $key = panel_page_asset_key($page);
    $relative = 'assets/' . $type . '/pages/' . $key . '.' . $type;
    $absolute = dirname(__DIR__) . '/' . $relative;
    if (!is_file($absolute)) {
        return null;
    }
    return [
        'key' => $key,
        'url' => rtrim($baseUrl, '/') . '/' . $relative,
        'version' => (int)@filemtime($absolute),
    ];
}
