import { ReactNode } from 'react';
import { MemoryRouter, Routes, Route, useLocation } from 'react-router-dom';
import { vi } from 'vitest';

/**
 * 测试路由器的包装组件
 */
export function TestRouter({
  children,
  initialPath = '/',
  routes = []
}: {
  children: ReactNode;
  initialPath?: string;
  routes?: Array<{ path: string; element: ReactNode }>;
}) {
  return (
    <MemoryRouter initialEntries={[initialPath]}>
      <Routes>
        {routes.map((route, index) => (
          // @ts-ignore
          <Route key={index} path={route.path} element={route.element} />
        ))}
        {/* @ts-ignore */}
        <Route path="*" element={children} />
      </Routes>
    </MemoryRouter>
  );
}

/**
 * 获取当前路由位置的Hook
 */
export function useTestLocation() {
  const location = useLocation();
  return location;
}

/**
 * 创建测试导航函数
 */
export function createTestNavigation() {
  return {
    navigate: vi.fn(),
    goBack: vi.fn(),
    goForward: vi.fn(),
    replace: vi.fn()
  };
}