import { defineComponent, h } from 'vue';
import { _useRouterViewDepth, useRoute } from './use';
import { resolveComponent } from './util';

/**
 * RouterView component for rendering matched route components.
 * Acts as a placeholder where route components are rendered based on the current route.
 * Supports nested routing with proper depth tracking using Vue's provide/inject mechanism.
 *
 * @param props - Component properties (RouterView accepts no props)
 * @param context - Vue setup context (not used)
 * @param context.slots - Component slots (not used)
 * @returns Vue render function that renders the matched route component at current depth
 *
 * @example
 *
 * ```vue
 * <template>
 *   <div id="app">
 *     <!-- Navigation links -->
 *     <nav>
 *       <RouterLink to="/">Home</RouterLink>
 *       <RouterLink to="/about">About</RouterLink>
 *       <RouterLink to="/users">Users</RouterLink>
 *     </nav>
 *
 *     <!-- Root level route components render here -->
 *     <RouterView />
 *   </div>
 * </template>
 * ```
 */
export const RouterView = defineComponent({
    name: 'RouterView',
    setup() {
        const route = useRoute();

        // Get current RouterView depth and automatically provide depth + 1 for children
        // This enables proper nested routing by tracking how deep we are in the component tree
        const depth = _useRouterViewDepth(true);

        return () => {
            // Get the matched route configuration at current depth
            // route.matched is an array of matched route configs from parent to child
            const matchedRoute = route.matched[depth];

            // Resolve the component, handling ES module format if necessary
            const component = matchedRoute
                ? resolveComponent(matchedRoute.component)
                : null;

            // Render the component with compilePath as key to force re-render when route config changes
            // Using compilePath ensures component is recreated when navigating to different route configs
            return component
                ? h(component, { key: matchedRoute.compilePath })
                : null;
        };
    }
});
