import {getAllCategoriesMetadataSync}from'./markdownLoader.js';// Utility to build navigation tree from folder structure in manifest
// Cache for manifest
let cachedManifest = null;
let manifestLoadPromise = null;
// Note: Direct import of JSON requires build-time configuration
// For now, we rely on async fetch or window.__CONTENT_MANIFEST__
// Function to load content manifest synchronously (for initial render)
function loadContentManifestSync() {
  if (cachedManifest) {
    return cachedManifest;
  }
  // In browser, try to load from window if available (set by initial page load)
  if (typeof window !== 'undefined' && window.__CONTENT_MANIFEST__) {
    cachedManifest = window.__CONTENT_MANIFEST__;
    console.log('✅ Manifest loaded from window.__CONTENT_MANIFEST__');
    return cachedManifest;
  }
  // Try to load from the static file path
  // This will only work if the manifest is already loaded
  return null;
}
// Function to load content manifest asynchronously
async function loadContentManifest() {
  if (cachedManifest) {
    return cachedManifest;
  }
  if (manifestLoadPromise) {
    return manifestLoadPromise;
  }
  const isBrowser = typeof window !== 'undefined' && typeof window.location !== 'undefined';
  if (!isBrowser) {
    return null;
  }
  manifestLoadPromise = (async () => {
    try {
      const baseUrl = window.location.origin;
      const possiblePaths = [`${baseUrl}/content/content-manifest.json`, '/content/content-manifest.json', './content/content-manifest.json', '../content/content-manifest.json'];
      for (const manifestPath of possiblePaths) {
        try {
          const response = await fetch(manifestPath);
          if (response.ok) {
            const manifest = await response.json();
            cachedManifest = manifest;
            // Also set on window for sync access
            if (typeof window !== 'undefined') {
              window.__CONTENT_MANIFEST__ = manifest;
            }
            console.log('✅ Successfully loaded content manifest from:', manifestPath);
            console.log('📦 Navigation items found:', manifest.navigationItems?.length || 0);
            return manifest;
          } else {
            console.log(`⚠️ Manifest not found at ${manifestPath} (status: ${response.status})`);
          }
        } catch (fetchError) {
          console.log(`⚠️ Error fetching ${manifestPath}:`, fetchError);
          continue;
        }
      }
      console.error('❌ Could not load content manifest from any path. Tried:', possiblePaths);
      return null;
    } catch (error) {
      console.error('❌ Error loading content manifest:', error);
      return null;
    } finally {
      manifestLoadPromise = null;
    }
  })();
  return manifestLoadPromise;
}
// Build navigation tree from manifest navigationItems (preferred method)
function buildNavigationFromManifest(manifest) {
  if (manifest && manifest.navigationItems && manifest.navigationItems.length > 0) {
    console.log('✅ Using navigationItems from manifest:', manifest.navigationItems.length, 'top-level items');
    return manifest.navigationItems;
  }
  if (manifest) {
    console.warn('⚠️ Manifest loaded but navigationItems is empty or missing');
  }
  return null;
}
// Build navigation tree from category metadata (fallback method)
function buildDocsNavigationFromCategories() {
  const categories = getAllCategoriesMetadataSync();
  const navItems = [];
  for (const category of categories) {
    // Create category node
    const categoryItem = {
      title: category.title,
      path: `/help/${category.id}`,
      children: [],
      isFile: false
    };
    // Create a flat list of articles under each category
    // Path format: /help/:categoryId/:slug (e.g., /help/adminide-modules/adminide-modules-account-auth0-login)
    const articleItems = category.articles.map(article => ({
      title: article.title,
      path: `/help/${category.id}/${article.slug}`,
      children: [],
      isFile: true
    }));
    // If there are articles, add them as children
    if (articleItems.length > 0) {
      categoryItem.children = articleItems;
    }
    navItems.push(categoryItem);
  }
  return navItems;
}
// Get navigation tree (cached, tries manifest first, falls back to categories)
let cachedNavigation = null;
function getDocsNavigation() {
  if (cachedNavigation) {
    return cachedNavigation;
  }
  // Try to load from manifest synchronously first
  const manifest = loadContentManifestSync();
  const navFromManifest = buildNavigationFromManifest(manifest);
  if (navFromManifest) {
    cachedNavigation = navFromManifest;
    console.log('✅ Navigation loaded from manifest (sync)');
    return cachedNavigation;
  }
  // Fallback to category-based navigation
  console.warn('⚠️ Falling back to category-based navigation (manifest not available sync)');
  cachedNavigation = buildDocsNavigationFromCategories();
  return cachedNavigation;
}
// Async version that ensures manifest is loaded
async function getDocsNavigationAsync() {
  // Try sync first
  const syncNav = getDocsNavigation();
  // Check if we already have manifest-based navigation
  const manifest = loadContentManifestSync();
  if (manifest && manifest.navigationItems && manifest.navigationItems.length > 0) {
    console.log('✅ Navigation already loaded from manifest');
    return syncNav;
  }
  // Load manifest asynchronously
  console.log('🔄 Loading manifest asynchronously...');
  const asyncManifest = await loadContentManifest();
  const navFromManifest = buildNavigationFromManifest(asyncManifest);
  if (navFromManifest) {
    cachedNavigation = navFromManifest;
    console.log('✅ Navigation loaded from manifest (async)');
    return cachedNavigation;
  }
  // Fallback to category-based navigation
  console.warn('⚠️ Falling back to category-based navigation (manifest not available async)');
  cachedNavigation = buildDocsNavigationFromCategories();
  return cachedNavigation;
}export{getDocsNavigation,getDocsNavigationAsync};//# sourceMappingURL=docsNavigation.js.map
