/**
 * Navigation utilities for docs viewer
 * 
 * These functions provide simple URL updates without state management.
 * The URL is the single source of truth - components derive their state from the URL.
 * 
 * URL Schema:
 * - #tab/page/{slug}                  → Doc page (docs view)
 * - #tab/endpoint/{id}                → API endpoint (docs view)
 * - #tab/endpoint/{id}/playground     → API endpoint (playground view)
 * - #tab/endpoint/{id}/notes          → API endpoint (notes view)
 * - #tab/page/{slug}/notes            → Doc page (notes view)
 */ /**
 * Navigate to a documentation page
 * @param tab - Tab ID (e.g., 'guides', 'graphql-api')
 * @param slug - Page slug (e.g., 'graphql/introduction')
 * @param view - View mode (docs, playground, notes). Defaults to 'docs'
 */ export function navigateToPage(tab, slug, view) {
    const viewSuffix = view && view !== 'docs' ? `/${view}` : '';
    window.location.hash = `${tab}/page/${slug}${viewSuffix}`;
}
/**
 * Navigate to an API endpoint or GraphQL operation
 * @param tab - Tab ID (e.g., 'api-reference', 'graphql-api')
 * @param id - Endpoint ID
 * @param view - View mode (docs, playground, notes). Defaults to 'docs'
 */ export function navigateToEndpoint(tab, id, view) {
    const viewSuffix = view && view !== 'docs' ? `/${view}` : '';
    window.location.hash = `${tab}/endpoint/${id}${viewSuffix}`;
}
/**
 * Navigate to a section within a page (scroll to element)
 * @param tab - Tab ID
 * @param sectionId - Section/heading ID to scroll to
 */ export function navigateToSection(tab, sectionId) {
    window.location.hash = `${tab}/section/${sectionId}`;
}
/**
 * Navigate to a tab (show default content for that tab)
 * @param tab - Tab ID
 */ export function navigateToTab(tab) {
    window.location.hash = tab;
}
/**
 * Switch view mode while keeping the current content
 * @param view - View mode to switch to
 */ export function switchView(view) {
    const hash = window.location.hash.slice(1);
    if (!hash) return;
    // Remove existing view suffix if present
    const cleanHash = hash.replace(/\/(playground|notes)$/, '');
    // Add new view suffix (unless switching to docs, which is the default)
    const viewSuffix = view !== 'docs' ? `/${view}` : '';
    window.location.hash = `${cleanHash}${viewSuffix}`;
}
/**
 * Clear current selection, optionally staying on a tab
 * @param tab - Optional tab to stay on
 */ export function clearSelection(tab) {
    if (tab) {
        window.location.hash = tab;
    } else {
        // Remove hash entirely using pushState to avoid scroll jump
        window.history.pushState(null, '', window.location.pathname + window.location.search);
    }
}
/**
 * Update URL without triggering navigation (for history management)
 * @param hash - Hash value (without #)
 */ export function updateUrlSilently(hash) {
    window.history.replaceState(null, '', `#${hash}`);
}
/**
 * Push a new URL to history (for back/forward navigation)
 * @param hash - Hash value (without #)
 */ export function pushUrl(hash) {
    window.history.pushState(null, '', `#${hash}`);
}
/**
 * Build hash URL strings (for href attributes)
 */ export const hashUrls = {
    page: (tab, slug, view)=>{
        const viewSuffix = view && view !== 'docs' ? `/${view}` : '';
        return `#${tab}/page/${slug}${viewSuffix}`;
    },
    endpoint: (tab, id, view)=>{
        const viewSuffix = view && view !== 'docs' ? `/${view}` : '';
        return `#${tab}/endpoint/${id}${viewSuffix}`;
    },
    section: (tab, sectionId)=>`#${tab}/section/${sectionId}`,
    tab: (tab)=>`#${tab}`
};
/**
 * Parse a hash URL into its components
 * @param hash - Hash value (with or without #)
 * @returns Parsed components
 */ export function parseHashUrl(hash) {
    const cleanHash = hash.startsWith('#') ? hash.slice(1) : hash;
    if (!cleanHash) {
        return {
            tab: null,
            type: null,
            id: null,
            view: 'docs'
        };
    }
    const parts = cleanHash.split('/');
    const tab = parts[0] || null;
    const type = parts[1];
    // Check if last part is a view mode
    const lastPart = parts[parts.length - 1];
    const isViewSuffix = lastPart === 'playground' || lastPart === 'notes';
    const view = isViewSuffix ? lastPart : 'docs';
    // Get ID (everything between type and view, or type and end)
    const idParts = isViewSuffix ? parts.slice(2, -1) : parts.slice(2);
    const id = idParts.join('/') || null;
    // Validate type
    const validTypes = [
        'page',
        'endpoint',
        'section'
    ];
    const validatedType = type && validTypes.includes(type) ? type : null;
    return {
        tab,
        type: validatedType,
        id,
        view
    };
}
/**
 * Check if two hash URLs point to the same content (ignoring view mode)
 * @param hash1 - First hash
 * @param hash2 - Second hash
 * @returns True if they point to the same content
 */ export function isSameContent(hash1, hash2) {
    const parsed1 = parseHashUrl(hash1);
    const parsed2 = parseHashUrl(hash2);
    return parsed1.tab === parsed2.tab && parsed1.type === parsed2.type && parsed1.id === parsed2.id;
}
