resolve static method

List<TSidebarItem> resolve(
  1. List<TSidebarItem> items
)

Resolves relative paths and validates the sidebar item hierarchy.

Implementation

static List<TSidebarItem> resolve(List<TSidebarItem> items) {
  bool homeFound = false;
  final Set<String> routes = {};

  List<TSidebarItem> resolveInternal(List<TSidebarItem> items, {String? parentPath}) {
    return items.map((item) {
      // 1. Resolve Path
      String? resolvedRoute = item.route;

      if (resolvedRoute != null) {
        if (parentPath != null) {
          if (resolvedRoute.contains('/')) {
            throw ArgumentError(
              "TLayout: The child item's route '${item.route}' must be a single child path name only (no '/', no parent path, and no nested segments)",
            );
          }

          resolvedRoute = parentPath.endsWith('/') ? '$parentPath$resolvedRoute' : '$parentPath/$resolvedRoute';
        }
      }

      // 2. Validation: Multiple Homes
      if (item.home) {
        if (homeFound) {
          throw ArgumentError('TLayout: Multiple items marked as home. Only one item can be home.');
        }
        homeFound = true;
      }

      // 3. Validation: Bottom Bar Position
      if (item.bottomBarPosition != null) {
        if (item.bottomBarPosition! > 3) {
          throw ArgumentError('TLayout: Bottom bar position cannot be greater than 3.');
        }
        if (item.children?.isNotEmpty ?? false) {
          throw ArgumentError('TLayout: Bottom bar items cannot have children.');
        }
      }

      // 4. Validation: Duplicate Routes
      if (resolvedRoute != null && resolvedRoute.isNotEmpty && !resolvedRoute.contains(':')) {
        if (routes.contains(resolvedRoute)) {
          throw ArgumentError('TLayout: Duplicate route detected: $resolvedRoute');
        }
        routes.add(resolvedRoute);
      }

      // 5. Recursive children resolution
      final resolvedChildren = item.children != null ? resolveInternal(item.children!, parentPath: resolvedRoute) : null;

      return item.copyWith(
        route: resolvedRoute,
        children: resolvedChildren,
      );
    }).toList();
  }

  final resolvedItems = resolveInternal(items);

  // Default home if none found
  if (!homeFound && resolvedItems.isNotEmpty) {
    resolvedItems[0] = resolvedItems[0].copyWith(home: true);
  }

  return resolvedItems;
}