/**
 * 路由导航 API
 * 提供框架级别的路由导航能力，封装 react-router-dom 的导航功能
 */ import { logger } from "../../../utils";
import { getRouterLifecycleManager } from "../lifecycle";
import { getRouterMiddlewareManager } from "../middleware";
/**
 * 路由导航器
 */ export class RouterNavigation {
    /**
   * 导航到指定路径
   * 
   * @param to - 目标路径或路由名称
   * @param options - 导航选项
   */ static async navigate(to, options) {
        const routerManager = await import("../RouterManager").then((m)=>m.getRouterManager());
        const router = routerManager.getRouter();
        if (!router) {
            throw new Error('路由未初始化，无法导航');
        }
        // 获取当前路由信息
        const currentLocation = this.getCurrentRoute();
        // 构建目标路由位置
        const targetLocation = {
            path: to,
            query: options?.query
        };
        // 执行生命周期钩子
        const lifecycleManager = getRouterLifecycleManager();
        const beforeEachResult = await lifecycleManager.beforeEach(targetLocation, currentLocation);
        // 如果 beforeEach 返回 false，阻止导航
        if (beforeEachResult === false) {
            logger.debug('路由导航被阻止: beforeEach 返回 false');
            return;
        }
        // 如果 beforeEach 返回字符串，重定向到该路径
        if (typeof beforeEachResult === 'string') {
            logger.debug(`路由导航重定向: ${beforeEachResult}`);
            to = beforeEachResult;
        }
        // 执行中间件
        const middlewareManager = getRouterMiddlewareManager();
        const middlewareContext = {
            to: {
                ...targetLocation,
                path: to
            },
            from: currentLocation,
            options
        };
        const middlewareResult = await middlewareManager.execute(middlewareContext);
        // 如果中间件阻止导航
        if (!middlewareResult.allow) {
            if (middlewareResult.redirect) {
                logger.debug(`路由导航被中间件重定向: ${middlewareResult.redirect}`);
                // 递归调用 navigate 进行重定向
                return this.navigate(middlewareResult.redirect, options);
            }
            logger.debug(`路由导航被中间件阻止: ${middlewareResult.reason || '未知原因'}`);
            return;
        }
        // 执行导航
        try {
            if (options?.replace) {
                router.navigate(to, {
                    replace: true,
                    state: options.state
                });
            } else {
                router.navigate(to, {
                    state: options?.state
                });
            }
            // 执行 afterEach 钩子
            await lifecycleManager.afterEach({
                ...targetLocation,
                path: to
            }, currentLocation);
        } catch (error) {
            logger.error('路由导航失败', error);
            throw error;
        }
    }
    /**
   * 获取当前路由信息
   * 
   * @returns 当前路由位置信息
   */ static getCurrentRoute() {
        // 从 window.location 获取当前路由信息
        const pathname = window.location.pathname;
        const search = window.location.search;
        const hash = window.location.hash;
        // 解析查询参数
        const query = {};
        if (search) {
            const params = new URLSearchParams(search);
            params.forEach((value, key)=>{
                query[key] = value;
            });
        }
        return {
            path: pathname + hash,
            query
        };
    }
    /**
   * 返回上一页
   * 
   * @param fallback - 如果没有历史记录，跳转到该路径
   */ static async goBack(fallback) {
        if (window.history.length > 1) {
            window.history.back();
        } else if (fallback) {
            await this.navigate(fallback);
        } else {
            logger.warn('没有历史记录，无法返回');
        }
    }
    /**
   * 前进到下一页
   */ static goForward() {
        window.history.forward();
    }
    /**
   * 替换当前路由
   * 
   * @param to - 目标路径
   * @param options - 导航选项
   */ static async replace(to, options) {
        return this.navigate(to, {
            ...options,
            replace: true
        });
    }
}

//# sourceMappingURL=RouterNavigation.js.map