'use client';
import { jsx as _jsx } from "react/jsx-runtime";
import { createContext, useContext, useCallback } from 'react';
const NavigationContext = /*#__PURE__*/ createContext(null);
/**
 * Helper to find request by operationId (name) or ID in collection
 */ function findRequestByOperationIdOrId(collection, operationIdOrId) {
    // Normalize the search term (handle both snake_case and the formatted name)
    const normalizedSearch = operationIdOrId.toLowerCase().replace(/[\s-]/g, '_');
    // Check direct requests
    for (const request of collection.requests){
        // Check by ID
        if (request.id === operationIdOrId) return request;
        // Check by name (operationId) - exact match
        if (request.name.toLowerCase().replace(/[\s-]/g, '_') === normalizedSearch) return request;
        // Check by name - partial match (for formatted names like "Post Carts" -> "post_carts")
        const requestNameNormalized = request.name.toLowerCase().replace(/[\s-]/g, '_');
        if (requestNameNormalized === normalizedSearch) return request;
    }
    // Check folders recursively
    for (const folder of collection.folders){
        const request = findRequestByOperationIdOrId(folder, operationIdOrId);
        if (request) return request;
    }
    return null;
}
export function NavigationProvider({ children, collection, onSelectRequest }) {
    const navigateToEndpoint = useCallback((operationIdOrId)=>{
        if (!collection) {
            console.warn('[Navigation] No collection available');
            return;
        }
        const request = findRequestByOperationIdOrId(collection, operationIdOrId);
        if (request) {
            console.log('[Navigation] Navigating to endpoint:', request.name);
            onSelectRequest(request);
        } else {
            console.warn('[Navigation] Endpoint not found:', operationIdOrId);
        }
    }, [
        collection,
        onSelectRequest
    ]);
    return /*#__PURE__*/ _jsx(NavigationContext.Provider, {
        value: {
            navigateToEndpoint,
            collection
        },
        children: children
    });
}
export function useNavigation() {
    const context = useContext(NavigationContext);
    if (!context) {
        // Return a no-op function if not in provider (for backwards compatibility)
        return {
            navigateToEndpoint: ()=>{},
            collection: null
        };
    }
    return context;
}
