/**
 * @description 格式化返回路由
 * @param constantRoutes
 * @returns {*}
 */
export function filterAllRoutes(constantRoutes) {
  return constantRoutes.filter((route) => {
    if (route.component) {
      if (route.component === "Main") {
        route.component = (resolve) =>
          require(["@/components/layout/Main"], 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 menulist
 * @description 递归获取权限菜单
 * @returns {*}
 */
export function recursiveMenu(data, targetId = "root") {
  const menus = []
  if (data.length > 0) {
    data.forEach((item) => {
      if (item.parentId === targetId) {
        item.children = recursiveMenu(data, item.id)
        menus.push(item)
      }
    })
  }
  return menus
}
/**
 * @param menulist
 * @description 菜单格式化
 * @returns {*}
 */
export function routesFormat(list) {
  const constantRoutes = []
  list.map((item, index) => {
    if (item.component) {
      constantRoutes.push({
        path: item.path,
        component: "Main",
        name: item.name,
        children: [item],
      })
    } else {
      constantRoutes.push({
        path: item.path,
        component: "Main",
        name: item.name,
        meta: item.meta,
        children: item.children,
      })
    }
  })
  return filterAllRoutes(constantRoutes)
}

/**
 * @param d 菜单list
 * @description 菜单权限格式化
 * @returns {*}
 */

export function formatMenulist(d = [], type = 1) {
  const menulist = [] //菜单
  let permissionList = [] //权限按钮
  if (d && d.length > 0) {
    d.forEach((item) => {
      // type === 1 菜单
      if (item.menuType == 1) {
        if (item.subPermissions && item.subPermissions.length > 0) {
          item.children = formatMenulist(item.subPermissions)
        }
        menulist.push({
          ...item,
          id: item.id,
          name: item.route,
          meta: {
            title: item.name,
            icon: item.icon || "",
            noKeepAlive: !item.keepAlive,
          },
        })
      } else {
        // type === 2 按钮权限
        permissionList.push({
          ...item,
        })
        if (item.subPermissions && item.subPermissions.length > 0) {
          permissionList = permissionList.concat(
            formatMenulist(item.subPermissions, 2)
          )
        }
      }
    })
  }

  return type === 1 ? menulist : permissionList
}
