import { defineStore } from "pinia"
import { useUserStore } from "./user"

export interface Menu{
    name:string,
    icon:string,
    url:string,
    children?:Menu[]
}
export type MenuOwner = '*'|'F'|'V'|'A'|'E'|'M'
export interface MenuState {
    // freeUser: Menu[],
    // vipUser: Menu[],
    // enterpriseUser: Menu[],
    // enterpriseAdmin: Menu[]
    userMenu:Menu[]
    currentMenu: Menu|null
}

const menus:{menu:Menu,owner:MenuOwner}[] = [
    {menu:{name:'账号一览',icon:'IconEarth',url:'/u'},owner:'*'},
    {menu:{name:'订单/发票',icon:'IconCurrency',url:'/u/order'},owner:'*'},
    {menu:{name:'账户设置',icon:'IconProtect',url:'/u/setting'},owner:'*'},
    {menu:{name:'字体设置',icon:'IconAddText',url:'/m'},owner:'M'},
    {menu:{name:'国家设置',icon:'IconWorld',url:'/m/nation'},owner:'M'},
    {menu:{name:'颜色设置',icon:'IconPlatte',url:'/m/colour'},owner:'M'},
    {menu:{name:'模版管理',icon:'IconPageTemplate',url:'/m/TemplateManagement'},owner:'M'},
]

export const useMenuStore = defineStore('menu', {
    state: (): MenuState => ({
        // freeUser:[],
        // vipUser:[],
        // enterpriseAdmin:[],
        // enterpriseUser:[],
        userMenu:[],
        currentMenu:null
    }),
    getters:{
        getUserMenu:(state)=>{
            if (!state.userMenu || state.userMenu.length==0) {
                const userStore= useUserStore()
                const userInfo = userStore.getUser
                let userPermissions:MenuOwner[] = ['F','*']
                
                if(userInfo.it){
                    userPermissions.push('E')
                }
                if(userInfo.ia){
                    userPermissions.push('A')
                }
                if(userInfo.iv){
                    userPermissions.push('V')
                }
                if(userInfo.isTemplate){
                    userPermissions.push('M')
                }
                const findFreeMenu = menus.filter(x=>userPermissions.includes(x.owner))
                if(findFreeMenu){
                    state.userMenu = findFreeMenu.map(x=>x.menu)
                }
            }
            return state.userMenu
        },
        getTemplateMenu:(state)=>{
            if (!state.userMenu || state.userMenu.length==0) {
                const userStore= useUserStore()
                const userInfo = userStore.getUser
                let userPermissions:MenuOwner[] = ['M']
                
                const findFreeMenu = menus.filter(x=>userPermissions.includes(x.owner))
                if(findFreeMenu){
                    state.userMenu = findFreeMenu.map(x=>x.menu)
                }
            }
            return state.userMenu
        },
    },
    // actions:{
    //     setUserMenu()
    // }
})