<?php
$uri = uri_string();
$navConfig = config('Navigation');
$navPerms = $navPerms ?? new \App\Services\NavigationPermissionService();
$currentModule = null;
$bestPrefixLen = -1;

foreach ($navConfig->modules as $module) {
    foreach ($module['prefixes'] as $prefix) {
        if ($uri === $prefix || str_starts_with($uri, $prefix . '/')) {
            $len = strlen($prefix);
            if ($len > $bestPrefixLen) {
                $bestPrefixLen = $len;
                $currentModule = $module;
            }
        }
    }
}

if ($currentModule === null) {
    return;
}

$itemActive = static function (string $path, bool $exact = false) use ($uri): bool {
    if ($exact) {
        return $uri === $path;
    }

    return $uri === $path || str_starts_with($uri, $path . '/');
};

$groupActive = static function (array $group) use ($itemActive): bool {
    foreach ($group['items'] as $item) {
        if ($itemActive($item['path'], ! empty($item['exact']))) {
            return true;
        }
    }

    return false;
};

$groups = $navPerms->filterGroups($currentModule['groups'] ?? []);
$isAdmin = ! empty($isAdmin);

if ($groups === []) {
    return;
}
?>

<aside class="enterprise-sidebar d-none d-lg-block" aria-label="<?= esc($currentModule['label']) ?> navigation">
    <div class="enterprise-sidebar-label"><?= esc($currentModule['label']) ?></div>
    <nav class="enterprise-sidebar-nav">
        <?php foreach ($groups as $groupIndex => $group): ?>
            <?php
            $isOpen    = $groupActive($group);
            $groupId   = 'sidebar-group-' . preg_replace('/[^a-z0-9]+/i', '-', strtolower($group['label'])) . '-' . $groupIndex;
            $toggleId  = $groupId . '-toggle';
            ?>
            <div class="enterprise-sidebar-group<?= $isOpen ? ' enterprise-sidebar-group--open' : '' ?>">
                <button
                    type="button"
                    class="enterprise-sidebar-group-toggle<?= $isOpen ? ' active' : '' ?>"
                    id="<?= esc($toggleId) ?>"
                    data-bs-toggle="collapse"
                    data-bs-target="#<?= esc($groupId) ?>"
                    aria-expanded="<?= $isOpen ? 'true' : 'false' ?>"
                    aria-controls="<?= esc($groupId) ?>"
                >
                    <i class="fa-solid <?= esc($group['icon']) ?> enterprise-sidebar-icon"></i>
                    <span class="enterprise-sidebar-group-label"><?= esc($group['label']) ?></span>
                    <i class="fa-solid fa-chevron-down enterprise-sidebar-group-chevron"></i>
                </button>
                <div class="collapse<?= $isOpen ? ' show' : '' ?>" id="<?= esc($groupId) ?>">
                    <div class="enterprise-sidebar-group-items">
                        <?php foreach ($group['items'] as $item): ?>
                            <?php $active = $itemActive($item['path'], ! empty($item['exact'])); ?>
                            <a
                                href="<?= esc(base_url($item['path'])) ?>"
                                class="enterprise-sidebar-link<?= $active ? ' active' : '' ?>"
                                <?= $active ? 'aria-current="page"' : '' ?>
                            >
                                <i class="fa-solid <?= esc($item['icon']) ?> enterprise-sidebar-icon"></i>
                                <span><?= esc($item['label']) ?></span>
                            </a>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </nav>
</aside>
