import { isPathUrl } from '~uti/tools'
import { getToken } from '~src/api/util/auth'
import { getStoreValue } from '~uti/storage'
import { getTenantDetail } from '~api/other'
import { getArrValue, getObjValue } 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
            if (item?.code === 'to-archives-url') {
                const tenantId = getStoreValue('tenantId')
                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
                    }
                }
            }
            window.open(domain + '/#/auth?token=' + token, '_blank')
        } else {
            return getArrValue(item?.children)
        }
    }
}
