const modules = import.meta.glob('../views/**/*.vue')

const dynamicImport = (modules: any, component: string) => {
  const keys = Object.keys(modules)
  const matchKey = keys.find(key => {
    const k = '/' + key.replace('../views/', '')
    return k === component
  })
  return matchKey && modules[matchKey]
}

export const generatorDynamicRouter = (dynamicRouter: Array<any>) => {
  dynamicRouter.forEach((item: any) => {
    if (item.component) {
      item.component = dynamicImport(modules, item.component + '.vue')
    } else {
      delete item['component']
    }
    if (item.name) {
      item.meta = {}
      item.meta.title = item.name
    }
    if (item.children) {
      generatorDynamicRouter(item.children)
    }
  })
  return dynamicRouter
}
