import React, { useEffect, useMemo } from 'react';
import { Route, useLocation } from 'react-router-dom';
import { PublicRoute } from './PublicRoute';
import { PrivateRoute } from './PrivateRoute';
import { useRouteRegistry } from './routeRegistryStore';
const normalizePaths = (path) => {
    if (Array.isArray(path)) {
        return path.filter(Boolean);
    }
    return path ? [path] : [];
};
const mergeRouteConfig = (existing, next) => {
    var _a;
    if (!existing) {
        return next;
    }
    return Object.assign(Object.assign(Object.assign({}, existing), next), { isPublic: existing.isPublic || next.isPublic, attemptSilentAuth: (_a = existing.attemptSilentAuth) !== null && _a !== void 0 ? _a : next.attemptSilentAuth });
};
const collectRouteConfigs = (nodes) => {
    const routeMap = new Map();
    const visit = (children) => {
        React.Children.forEach(children, (child) => {
            var _a;
            if (!React.isValidElement(child)) {
                return;
            }
            const { props, type } = child;
            const paths = normalizePaths(props === null || props === void 0 ? void 0 : props.path);
            let config = null;
            if (type === PublicRoute) {
                config = {
                    isPublic: true,
                    attemptSilentAuth: (_a = props === null || props === void 0 ? void 0 : props.attemptSilentAuth) !== null && _a !== void 0 ? _a : false
                };
            }
            else if (type === PrivateRoute) {
                config = { isPublic: false };
            }
            else if (type === Route) {
                config = { isPublic: true };
            }
            if (config && paths && paths.length > 0) {
                paths.forEach((path) => {
                    const existing = routeMap.get(path);
                    routeMap.set(path, mergeRouteConfig(existing, config));
                });
            }
            if (props === null || props === void 0 ? void 0 : props.children) {
                visit(props.children);
            }
        });
    };
    visit(nodes);
    return Array.from(routeMap.entries()).map(([path, config]) => ({
        path,
        config
    }));
};
/**
 * Registers known routes ahead of auth/theme gating so public routes can bypass
 * the loading screens. Returns the current route config plus a convenience
 * `isPublicRoute` flag.
 */
export const useRouteRegistration = (children) => {
    const registerRoute = useRouteRegistry((state) => state.registerRoute);
    const setPath = useRouteRegistry((state) => state.setPath);
    const routeConfig = useRouteRegistry((state) => state.routeMap[state.currentPath]);
    const location = useLocation();
    useEffect(() => {
        // Update current path for non-routed mounts (e.g., initial render)
        setPath(location.pathname);
        const discoveredRoutes = collectRouteConfigs(children);
        discoveredRoutes.forEach(({ path, config }) => {
            registerRoute(path, config);
        });
    }, [children, location.pathname, registerRoute, setPath]);
    return {
        routeConfig,
        isPublicRoute: (routeConfig === null || routeConfig === void 0 ? void 0 : routeConfig.isPublic) === true
    };
};
//# sourceMappingURL=useRouteRegistration.js.map