import { getToken } from 'hc-vue3-ui'
import { getTenantDetail } from '~api/other'
import { getStoreValue } from '~src/utils/storage'
import { isPathUrl } from '~uti/tools'
import { getArrValue, getObjValue, newWindow } from 'js-fast-way'

export default class HcTopMenu {

    // 基础菜单
    static baseMenu = [
        'home', 'home-index', 'home-index-static', 'home-config', 'order-service', 'user-index', '403', '404', '500',
    ]

    static initMenu({ routes, menu, load, change }) {
        const topName = routes.matched[0]?.name
        if (this.baseMenu.includes(topName)) {
            load(topName)
            return false
        }
        for (let i = 0; i < menu.length; i++) {
            if (menu[i].code === topName) {
                change(topName, menu[i])
            }
        }
    }

    static async setMenuItem(item) {
        if (isPathUrl(item?.path)) {
            let token = getToken(), domain = item?.path
            const tenantId = getStoreValue('tenantId') ?? ''
            const projectId = getStoreValue('projectId') ?? ''
            const contractId = getStoreValue('contractId') ?? ''
            if (item?.code === 'to-client-url' || item?.code === 'to-archives-url') {
                if (tenantId === '000000' || !tenantId) {
                    domain = item?.path
                } else {
                    const { error, code, data } = await getTenantDetail(tenantId)
                    if (!error && code === 200) {
                        const url = getObjValue(data).domainUrl
                        domain = url ? url : item?.path
                    }
                }
            }
            //跳转到新页面
            newWindow(`${domain}/#/auth?token=${token}&tid=${tenantId}&pid=${projectId}&cid=${contractId}`)
        } else {
            return getArrValue(item?.children)
        }
    }
}
