import {
  DOCS_REGISTRY,
  getAllDocSections,
  getPublicDocSections,
  getSuperadminDocSections,
  findDocSection,
  findDocSectionInCategory,
  findDocPage
} from "@nextsparkjs/registries/docs-registry";
class DocsService {
  // ============================================================================
  // Registry Access
  // ============================================================================
  /**
   * Get the full documentation registry
   */
  static getRegistry() {
    return DOCS_REGISTRY;
  }
  /**
   * Get all documentation sections (public + superadmin)
   */
  static getAll() {
    return getAllDocSections();
  }
  /**
   * Get public documentation sections (for /docs)
   */
  static getPublic() {
    return getPublicDocSections();
  }
  /**
   * Get superadmin documentation sections (for /superadmin/docs)
   */
  static getSuperadmin() {
    return getSuperadminDocSections();
  }
  // ============================================================================
  // Section Operations
  // ============================================================================
  /**
   * Find a section by slug across all categories
   */
  static findSection(slug) {
    return findDocSection(slug);
  }
  /**
   * Find a section by slug in a specific category
   */
  static findSectionInCategory(slug, category) {
    return findDocSectionInCategory(slug, category);
  }
  /**
   * Check if a section exists
   */
  static sectionExists(slug) {
    return findDocSection(slug) !== void 0;
  }
  /**
   * Get all section slugs
   */
  static getSectionSlugs() {
    return getAllDocSections().map((section) => section.slug);
  }
  /**
   * Get section count
   */
  static getSectionCount() {
    return getAllDocSections().length;
  }
  // ============================================================================
  // Page Operations
  // ============================================================================
  /**
   * Find a page within a section
   */
  static findPage(sectionSlug, pageSlug) {
    return findDocPage(sectionSlug, pageSlug);
  }
  /**
   * Check if a page exists
   */
  static pageExists(sectionSlug, pageSlug) {
    return findDocPage(sectionSlug, pageSlug) !== void 0;
  }
  /**
   * Get all pages in a section
   */
  static getPages(sectionSlug) {
    const section = findDocSection(sectionSlug);
    return (section == null ? void 0 : section.pages) ?? [];
  }
  /**
   * Get total page count across all sections
   */
  static getPageCount() {
    return getAllDocSections().reduce(
      (total, section) => total + section.pages.length,
      0
    );
  }
  /**
   * Get all pages as a flat array
   */
  static getAllPages() {
    return getAllDocSections().flatMap((section) => section.pages);
  }
  // ============================================================================
  // Path Resolution
  // ============================================================================
  /**
   * Build the URL path for a page
   *
   * @param category - 'public' or 'superadmin'
   * @param sectionSlug - Section slug
   * @param pageSlug - Page slug
   * @returns URL path string
   */
  static buildPageUrl(category, sectionSlug, pageSlug) {
    if (category === "superadmin") {
      return `/superadmin/docs/${sectionSlug}/${pageSlug}`;
    }
    return `/docs/${sectionSlug}/${pageSlug}`;
  }
  /**
   * Build the URL path for a section
   *
   * @param category - 'public' or 'superadmin'
   * @param sectionSlug - Section slug
   * @returns URL path string
   */
  static buildSectionUrl(category, sectionSlug) {
    if (category === "superadmin") {
      return `/superadmin/docs/${sectionSlug}`;
    }
    return `/docs/${sectionSlug}`;
  }
  /**
   * Parse a URL path to extract category, section, and page
   *
   * @param pathname - URL pathname (e.g., '/docs/getting-started/intro')
   * @returns Parsed path info or null if invalid
   */
  static parsePath(pathname) {
    const parts = pathname.split("/").filter(Boolean);
    if (parts[0] === "docs") {
      return {
        category: "public",
        sectionSlug: parts[1] ?? null,
        pageSlug: parts[2] ?? null
      };
    }
    if (parts[0] === "superadmin" && parts[1] === "docs") {
      return {
        category: "superadmin",
        sectionSlug: parts[2] ?? null,
        pageSlug: parts[3] ?? null
      };
    }
    return null;
  }
  // ============================================================================
  // Search & Filter
  // ============================================================================
  /**
   * Search across all documentation
   *
   * @param query - Search query string
   * @param category - Optional category filter
   * @returns Filtered sections with matching pages
   */
  static search(query, category) {
    const trimmedQuery = query.trim().toLowerCase();
    if (!trimmedQuery) {
      return category ? DOCS_REGISTRY[category] : getAllDocSections();
    }
    const sections = category ? DOCS_REGISTRY[category] : getAllDocSections();
    return sections.map((section) => {
      const sectionMatches = section.title.toLowerCase().includes(trimmedQuery);
      const matchingPages = section.pages.filter(
        (page) => page.title.toLowerCase().includes(trimmedQuery)
      );
      if (sectionMatches || matchingPages.length > 0) {
        return {
          ...section,
          pages: sectionMatches ? section.pages : matchingPages
        };
      }
      return null;
    }).filter((section) => section !== null);
  }
  // ============================================================================
  // Navigation Helpers
  // ============================================================================
  /**
   * Get the first page of the first section (landing page)
   *
   * @param category - 'public' or 'superadmin'
   * @returns First page or undefined
   */
  static getFirstPage(category) {
    var _a;
    const sections = DOCS_REGISTRY[category];
    return (_a = sections[0]) == null ? void 0 : _a.pages[0];
  }
  /**
   * Get previous and next pages for navigation
   *
   * @param category - 'public' or 'superadmin'
   * @param sectionSlug - Current section slug
   * @param pageSlug - Current page slug
   * @returns Previous and next page info
   */
  static getNavigation(category, sectionSlug, pageSlug) {
    const sections = DOCS_REGISTRY[category];
    const allPages = [];
    sections.forEach((section) => {
      section.pages.forEach((page) => {
        allPages.push({ section, page });
      });
    });
    const currentIndex = allPages.findIndex(
      (item) => item.section.slug === sectionSlug && item.page.slug === pageSlug
    );
    if (currentIndex === -1) {
      return { prev: null, next: null };
    }
    return {
      prev: currentIndex > 0 ? allPages[currentIndex - 1] : null,
      next: currentIndex < allPages.length - 1 ? allPages[currentIndex + 1] : null
    };
  }
  // ============================================================================
  // Metadata
  // ============================================================================
  /**
   * Get documentation metadata/stats
   */
  static getMetadata() {
    const publicSections = DOCS_REGISTRY.public.length;
    const superadminSections = DOCS_REGISTRY.superadmin.length;
    const publicPages = DOCS_REGISTRY.public.reduce(
      (sum, s) => sum + s.pages.length,
      0
    );
    const superadminPages = DOCS_REGISTRY.superadmin.reduce(
      (sum, s) => sum + s.pages.length,
      0
    );
    return {
      publicSections,
      superadminSections,
      totalSections: publicSections + superadminSections,
      publicPages,
      superadminPages,
      totalPages: publicPages + superadminPages,
      sectionSlugs: getAllDocSections().map((s) => s.slug)
    };
  }
}
const getDocsRegistry = () => DocsService.getRegistry();
const getAllDocs = () => DocsService.getAll();
const getPublicDocs = () => DocsService.getPublic();
const getSuperadminDocs = () => DocsService.getSuperadmin();
const findDocsSection = (slug) => DocsService.findSection(slug);
const findDocsPage = (sectionSlug, pageSlug) => DocsService.findPage(sectionSlug, pageSlug);
const searchDocs = (query, category) => DocsService.search(query, category);
export {
  DocsService,
  findDocsPage,
  findDocsSection,
  getAllDocs,
  getDocsRegistry,
  getPublicDocs,
  getSuperadminDocs,
  searchDocs
};
