import { defineStore } from 'pinia'
import { asyncRoutes, basicRoutes } from '@/router/routes'
import { getMenu } from '@/api/auth/index'
import Layout from '@/layout/index.vue'
import modules from '@/utils/module.js'

/**
 * @description:
 * @param {*} route
 * @param {*} role
 * @return {*}
 */
function hasPermission(route, role) {
  // const routeRole = route.meta?.role ? route.meta.role : []
  // if (!role.length || !routeRole.length) {
  //   return false
  // }
  // return role.some((item) => routeRole.includes(item))
  return true
}

/**
 * @description: 过滤权限路由
 * @param {*} routes
 * @param {*} role
 * @return {*}
 */
function filterAsyncRoutes(routes = [], role) {
  const ret = []
  routes.forEach((route) => {
    if (hasPermission(route, role)) {
      const curRoute = {
        ...route,
        children: []
      }
      if (route.children && route.children.length) {
        curRoute.children = filterAsyncRoutes(route.children, role)
      } else {
        Reflect.deleteProperty(curRoute, 'children')
      }
      ret.push(curRoute)
    }
  })
  return ret
}

/**
 * @description:
 * @param {*} routes
 * @return {*}
 */
function dataArrayToRoutes(routes) {
  const res = []
  routes.forEach(item => {
    const tmp = { ...item }
    // // 如果有component配置
    // if (tmp.component) {
    //   // Layout引入
    //   if (tmp.component === 'Layout') {
    //     tmp.component = Layout
    //   } else {
    //     const sub_view = tmp.component.replace(/^\/*/g, '')
    //     const component = `../${sub_view}.vue`
    //     tmp.component = modules[component]
    //   }
    //   if (tmp.children) {
    //     tmp.children = dataArrayToRoutes(tmp.children)
    //   }
    // }
    // 如果pid为0
    if (tmp.pid === 0) {
      // Layout引入
      tmp.component = Layout
      if (tmp.children) {
        tmp.children = dataArrayToRoutes(tmp.children)
      }
    } else {
      const sub_view = tmp.component.replace(/^\/*/g, '')
      const component = `../${sub_view}.vue`
      tmp.component = modules[component]
    }
    tmp.name = tmp.title
    tmp.meta = {
      ...tmp.meta,
      title: tmp.meta.title || tmp.title
    }
    res.push(tmp)
  })
  return res
}

export const usePermissionStore = defineStore('permission', {
  state() {
    return {
      accessRoutes: []
    }
  },
  getters: {
    routes() {
      return basicRoutes.concat(this.accessRoutes)
    },
    permissionRoutes() {
      return this.accessRoutes
    }
  },
  actions: {
    generateRoutesMock(role = []) {
      const accessRoutes = filterAsyncRoutes(asyncRoutes, role)
      this.accessRoutes = accessRoutes
      return accessRoutes
    },
    async generateRoutes() {
      try {
        const res = await getMenu()
        if (res.code === 0) {
          const result = dataArrayToRoutes(res.data)
          console.log(result)
          this.accessRoutes = result
          return Promise.resolve(result)
        } else {
          return Promise.reject(res.message)
        }
      } catch (error) {
        console.error(error)
        return Promise.reject(error.message)
      }
    }
  }
})
