import {
  registerNavSection,
  registerNavItem,
  getNavSections,
  getNavItems,
} from './registry.js';
import { isDevkitEnabled } from '../../shared/lib/appConfig.js';
import { PERMISSIONS } from '@zen/core/users/constants';

// Sections et items core — enregistrés à l'import de ce module.
registerNavSection({ id: 'dashboard', title: 'Tableau de bord', icon: 'DashboardSquare03Icon', order: 10 });
registerNavSection({ id: 'system',    title: 'Utilisateurs',    icon: 'UserMultiple02Icon',     order: 20 });

registerNavItem({ id: 'dashboard', label: 'Tableau de bord', icon: 'DashboardSquare03Icon', href: '/admin/dashboard', sectionId: 'dashboard', order: 10 });
registerNavItem({ id: 'users',     label: 'Utilisateurs',   icon: 'UserMultiple02Icon',    href: '/admin/users',     sectionId: 'system',    order: 10, permission: PERMISSIONS.USERS_VIEW   });
registerNavItem({ id: 'roles',     label: 'Rôles',          icon: 'Crown03Icon',           href: '/admin/roles',     sectionId: 'system',    order: 20, permission: PERMISSIONS.ROLES_VIEW   });
registerNavItem({ id: 'settings',  label: 'Paramètres',     icon: 'Settings02Icon',        href: '/admin/settings',  position: 'bottom',     order: 10 });

if (isDevkitEnabled()) {
  registerNavSection({ id: 'devkit', title: 'DevKit', icon: 'Wrench01Icon', order: 90 });
  registerNavItem({ id: 'devkit-components', label: 'Composants', icon: 'Layers01Icon', href: '/admin/devkit/components', sectionId: 'devkit', order: 10 });
  registerNavItem({ id: 'devkit-icons', label: 'Icônes', icon: 'Image01Icon', href: '/admin/devkit/icons', sectionId: 'devkit', order: 20 });
}

/**
 * Build sections for AdminSidebar. Items are sérialisables (pas de composants),
 * icônes en chaînes résolues côté client.
 * @param {string} pathname
 * @param {string[]} [userPermissions] - Permissions de l'utilisateur connecté ; les items
 *   avec un champ `permission` sont masqués si la permission n'est pas présente.
 */
export function buildNavigationSections(pathname, userPermissions = []) {
  const sections = getNavSections();
  const items = getNavItems().filter(item => {
    if (item.position === 'bottom') return false;
    if (item.permission && !userPermissions.includes(item.permission)) return false;
    return true;
  });

  const bySection = new Map();
  for (const item of items) {
    const list = bySection.get(item.sectionId) || [];
    list.push({
      name: item.label,
      href: item.href,
      icon: item.icon,
      current: pathname === item.href || pathname.startsWith(item.href + '/'),
    });
    bySection.set(item.sectionId, list);
  }

  return sections
    .filter(s => bySection.has(s.id))
    .map(s => ({ id: s.id, title: s.title, icon: s.icon, items: bySection.get(s.id) }));
}

/**
 * Build the list of bottom-pinned nav items for AdminSidebar.
 */
export function buildBottomNavItems(pathname) {
  return getNavItems()
    .filter(item => item.position === 'bottom')
    .sort((a, b) => (a.order ?? 0) - (b.order ?? 0))
    .map(item => ({
      name: item.label,
      href: item.href,
      icon: item.icon,
      current: pathname === item.href || pathname.startsWith(item.href + '/'),
    }));
}
