<?php

use MintyPHP\Service\Docs\DocsCatalogService;

$layoutAuth = is_array($viewAuth['layout'] ?? null) ? $viewAuth['layout'] : [];
$layoutNav = is_array($layoutNav ?? null) ? $layoutNav : [];
$helpNav = is_array($layoutNav['help-center.nav'] ?? null) ? $layoutNav['help-center.nav'] : [];
$canViewDocs = !empty($layoutAuth['can_view_docs']);
$canViewApiDocs = !empty($helpNav['can_view_api_docs']);
$canViewSystemInfo = !empty($layoutAuth['can_view_system_info']);

$docsDefaultSlug = $canViewDocs ? DocsCatalogService::defaultSlug() : '';

$helpNavGroups = [
    [
        'key' => 'help-documentation',
        'label' => t('Documentation'),
        'icon' => 'bi-book',
        'items' => [
            [
                'label' => t('Documentation'),
                'path' => $docsDefaultSlug !== '' ? 'admin/docs/' . $docsDefaultSlug : '',
                'active' => navActive('admin/docs', true),
                'visible' => $canViewDocs && $docsDefaultSlug !== '',
            ],
            [
                'label' => t('API docs'),
                'path' => 'admin/api-docs',
                'active' => navActive('admin/api-docs', true),
                'visible' => $canViewApiDocs,
            ],
        ],
    ],
    [
        'key' => 'help-system',
        'label' => t('System'),
        'icon' => 'bi-info-circle',
        'items' => [
            [
                'label' => t('System status'),
                'path' => 'admin/system-info',
                'active' => navActive('admin/system-info', true),
                'visible' => $canViewSystemInfo,
            ],
        ],
    ],
];
?>
<ul class="app-sidebar-admin-nav" aria-label="<?php e(t('Help')); ?>">
<?php foreach ($helpNavGroups as $group):
    $items = array_values(array_filter($group['items'], static fn ($item) => !empty($item['visible'])));
    if (!$items) { continue; }
    $groupIsActive = false;
    foreach ($items as $item) {
        $activeState = $item['active'] ?? [];
        if (!empty($activeState['isActive'])) { $groupIsActive = true; break; }
    }
?>
    <li class="app-sidebar-group app-sidebar-admin-group">
        <details data-details-key="<?php e($group['key']); ?>"<?php if ($groupIsActive): ?> open<?php endif; ?>>
            <summary>
                <i class="bi <?php e($group['icon']); ?>"></i>
                <span><?php e($group['label']); ?></span>
            </summary>
            <ul>
                <?php foreach ($items as $item):
                    $active = $item['active'] ?? ['class' => '', 'aria' => ''];
                ?>
                    <li>
                        <a href="<?php e(lurl($item['path'])); ?>" class="<?php e($active['class'] ?? ''); ?>" <?php echo $active['aria'] ?? ''; // raw-html-ok: pre-built ARIA attribute from navActive() ?>>
                            <?php e($item['label']); ?>
                        </a>
                    </li>
                <?php endforeach; ?>
            </ul>
        </details>
    </li>
<?php endforeach; ?>
</ul>
