/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description all模式渲染后端返回路由
 * @param constantRoutes
 * @returns {*}
 */
export function filterAllRoutes(constantRoutes) {
  return constantRoutes.filter((route) => {
    if (route.component) {
      if (route.component === "Layout") {
        route.component = (resolve) => require(["@/layouts"], resolve)
      } else if (route.component === "EmptyLayout") {
        route.component = (resolve) =>
          require(["@/layouts/EmptyLayout"], resolve)
      } else {
        let path = "views/" + route.component
        if (
          new RegExp("^/views/.*$").test(route.component) ||
          new RegExp("^views/.*$").test(route.component)
        ) {
          path = route.component
        } else if (new RegExp("^/.*$").test(route.component)) {
          path = "views" + route.component
        } else {
          path = "views/" + route.component
        }
        route.component = (resolve) => require([`@/${path}`], resolve)
      }
    }
    if (route.children && route.children.length) {
      route.children = filterAllRoutes(route.children)
    }
    if (route.children && route.children.length === 0) {
      delete route.children
    }
    return true
  })
}

/**
 * @param list
 * @description 根据父级ID递归获取树状结构
 * @returns {*}
 */
export function translateListToTree(data) {
  const menus = []
  if (data.length > 0) {
    let map = {}
    data.forEach((item) => {
      map[item.id] = item
    })

    data.forEach((item) => {
      let parent = map[item.parentId]
      if (parent) {
        ;(parent.children || (parent.children = [])).push(item)
      } else {
        menus.push(item)
      }
    })
  }
  return menus
}
/**
 * @param menulist
 * @description 菜单格式化
 * @returns {*}
 */
export function routesFormat(menulist) {
  const list = translateListToTree(
    menulist.map((item, index) => {
      return {
        id: item.id,
        parentId: item.parentId,
        name: item.route,
        path: item.path,
        component: item.component,
        meta: {
          title: item.name,
          icon: item.icon || "",
          noKeepAlive: !item.keepAlive,
        },
      }
    })
  )
  const constantRoutes = []
  list.map((item, index) => {
    if (item.component) {
      if (item.component === "EmptyLayout") {
        constantRoutes.push({
          path: item.path,
          component: item.component,
          meta: item.meta,
          children: item.children,
        })
      } else {
        constantRoutes.push({
          path: item.path,
          component: "Layout",
          children: [item].flat(Infinity),
        })
      }
    } else {
      constantRoutes.push({
        path: item.path,
        component: "Layout",
        meta: item.meta,
        children: item.children,
      })
    }
  })
  return filterAllRoutes(constantRoutes)
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description 判断当前路由是否包含权限
 * @param permissions
 * @param route
 * @returns {boolean|*}
 */
function hasPermission(permissions, route) {
  if (route.meta && route.meta.permissions) {
    return permissions.some((role) => route.meta.permissions.includes(role))
  } else {
    return true
  }
}

/**
 * @copyright chuzhixin 1204505056@qq.com
 * @description intelligence模式根据permissions数组拦截路由
 * @param routes
 * @param permissions
 * @returns {[]}
 */
export function filterAsyncRoutes(routes, permissions) {
  const finallyRoutes = []
  routes.forEach((route) => {
    const item = {
      ...route,
    }
    if (hasPermission(permissions, item)) {
      if (item.children) {
        item.children = filterAsyncRoutes(item.children, permissions)
      }
      finallyRoutes.push(item)
    }
  })
  return finallyRoutes
}
