import { Navigate } from "react-router-dom"
import { getToken } from "../utils/auth"

const Views = import.meta.glob('../view/**/*.tsx')
// 判断是否有对应路径的映射文件
const hasMapFile = (filePath: string): boolean => {
  return Views[`../view/${filePath}.tsx`] ? true : false
}

/**
 * @author {温华}
 * @description 路由拦截组件
 * @param {children: ReactDOM, filePath: string(文件路径)}  
 */
export default function RouteInterception({ children, filePath = "" }: any) {
  if (getToken()) {
    if (filePath !== "") {
      if (hasMapFile(filePath)) {
        return children
      } else {
        return <Navigate to='/404' />
      }
    }
    return children
  } else {
    return <Navigate to='/login' replace={true} />
  }
}