import mxConfig from "@/common/mxConfig";

export function openUrl(url) {
    if (typeof plus == 'undefined') {
        if (typeof window?.open === 'function') {
            window.open(url)
            return true
        }
        return false
    }
    plus.runtime.openURL(url)
    return true
}

export function openAppLink(appKey) {
    if (typeof plus == 'undefined') return
    const linkConfig = mxConfig.sysAppLinks[appKey]
    if (!linkConfig) return
    const platform = plus.os.name
    const platformLink = linkConfig[platform]
    if (!platformLink) return
    if (platformLink.url) {
        plus.runtime.openURL(platformLink.url)
    } else {
        console.log(platformLink, linkConfig)
        if (plus.runtime.isApplicationExist(platformLink)) {
            plus.runtime.launchApplication(platformLink, function(e) {
                console.log('launchApplication error:', e)
            })
        } else {
            plus.runtime.openURL(linkConfig.failUrl)
        }
    }
}
