import { defineStore } from "pinia"
import { store } from '@/stores'
import { staticRouter, exceptionRouter } from '@/router/modules/staticRouter'
import type { AppRouter } from '@/router/config/index'
import { generator } from '@/router/func/index'
import { baseRouter } from '@/router/base/index'
import { handleTree } from "@/utils/util"
import { Row } from "ant-design-vue"

interface router {
  routerArr: AppRouter[]
  asyncRouter: ExtraObj[]
}


export const useRouterStore = defineStore('router', {
  state: (): router => ({
    routerArr: [],
    asyncRouter: []
  }),
  getters: {
    ROUTERS(state):AppRouter[]  {
      return state.routerArr
    },
    ASYNCROUTERS(state): ExtraObj[]  {
      return state.asyncRouter
    },
  },
  actions: {
    setRouter(data: AppRouter[] ) {
      this.routerArr = data
    },
    async loadRouter(): Promise<AppRouter[]> {
      let routers:AppRouter[] = []
      return new Promise(async (resolve) => {

        // 组装
        const menuNav: ExtraObj[] = []
        baseRouter.children = staticRouter.concat() as AppRouter[]
        menuNav.push(baseRouter)
        menuNav.push(...exceptionRouter)
        // menuNav.push(...globalRouter)
        routers = await generator(menuNav, null)
        console.log('routers', routers)
        this.routerArr = routers
        resolve(routers)
      })
    }
  },
})

export const useRouterStoreWithOut = () => {
  return useRouterStore(store)
}
