import { shallowRef } from 'vue'
//import { queryNewFuncmenu } from '@/services/api/common'
import { queryAccountMenu } from '@/services/api/account'
import { defineStore } from '../store'
import { sessionData } from '../storage'

export const useMenuStore = defineStore(() => {
    const loading = shallowRef(false)
    const userRoutes = sessionData.getRef('userRoutes')

    /** 获取用户菜单列表 */
    const getUserMenuList = async () => {
        loading.value = true
        try {
            const res = await queryAccountMenu()
            userRoutes.value = res.data
        } finally {
            loading.value = false
        }
        // try {
        //     const res_1 = await queryNewFuncmenu({
        //         menutype: '5',
        //     })
        //     // 扁平列表树形化
        //     const arrayToTree = (list_1: Ermcp.NewFuncmenuRsp[], code: null | string = null) => {
        //         const getChildren = (parent_1: null | string) => {
        //             const result_1: Ermcp.UserRoutes[] = []
        //             list_1.forEach((e) => {
        //                 if (e.parentcode === parent_1) {
        //                     const item = {
        //                         id: 0,
        //                         authType: e.authtype,
        //                         title: e.resourcename,
        //                         code: e.resourcecode,
        //                         urlType: e.urltype,
        //                         url: e.url || '',
        //                         component: e.component,
        //                         icon: e.iconame,
        //                         buttonName: e.buttonname,
        //                         buttonType: e.buttontype,
        //                         className: '',
        //                         sort: e.sort,
        //                         hidden: Boolean(e.hidden),
        //                         remark: e.remark,
        //                         children: getChildren(e.resourcecode),
        //                     }
        //                     result_1.push(item)
        //                 }
        //             })
        //             return result_1
        //         }
        //         return getChildren(code)
        //     }
        //     userRoutes.value = arrayToTree(res_1.data)
        // } finally {
        //     loading.value = false
        // }
    }

    return {
        loading,
        userRoutes,
        getUserMenuList,
    }
})