async function queryWindowManagement() {
    return await navigator.permissions.query({ name: 'window-management' });;
}
async function getPermission() {
    const permission = await queryWindowManagement();
    console.log(permission.state, 'permission.state')
    if (permission.state === "granted") { // 已经授权
        return true
    } else if (permission.state === "prompt") { // 询问是否授权
        // 请求授权
        if (navigator.permissions.request) {
            navigator.permissions.request({ name: 'window-management' })
        }
        return false
    } else if (permission.state === "denied") {
        // 权限被拒绝
        return false
    }
}
function getScreen() {
    return new Promise((resolve, reject) => {
        let multiScreen = []
        if (window.getScreenDetails) {
            let getScreensProsime1 = window.getScreenDetails();
            getScreensProsime1.then((c) => {
                if (c instanceof Array) {
                    multiScreen = c
                } else if (c instanceof Object) {
                    multiScreen = c.screens
                } else {
                    multiScreen = []
                }
                resolve(multiScreen)
            }).catch((err) => {
                reject(err)
            });
        } else if (window.getScreens) {
            let getScreensProsime = window.getScreens();
            getScreensProsime.then((c) => {
                if (c instanceof Array) {
                    multiScreen = c
                } else if (c instanceof Object) {
                    multiScreen = c.screens
                } else {
                    multiScreen = []
                }
                resolve(multiScreen)
            }).catch((err) => {
                reject(err)
            })
        }
    })

}
export async function openWindow(url, name, Skip = false) {
    // 判断浏览器是否兼容
    // 高版本的谷歌，edge不支持跨屏，需要降低浏览器版本86.0版
    if (!window.getScreens && !window.getScreenDetails) {
        console.log('你的浏览器版本不支持多屏展示功能！');
        return window.open(url, name);
    }
    let permission = await getPermission()
    if (!permission) {
        // alert('使用多屏功能请先进行授权')
        if (Skip) {
            return window.open(url, name);
        }
        return false
    }
    let multiScreen = await getScreen()
    console.log(multiScreen, 'multiScreen')
    // 判断是否2个屏幕
    if (multiScreen.length < 2) {
        console.log('请接入多个显示屏！');
        return window.open(url, name);
    }
    console.log(screen, 'screen')
    // 获取当前屏幕availLeft信息和所有信息比对，取另一个屏幕数据
    let currentAvailLeft = screen.availLeft ? screen.availLeft : '0'
    let newCurr = multiScreen.find((t) => t.availLeft != currentAvailLeft)
    console.log(newCurr, 'newCurr')
    let fulls = ''
    for (let key in newCurr) {
        fulls += `${key}=${(newCurr[key] || newCurr[key] === 0) ? newCurr[key] : 0},`
    }
    console.log(fulls, 'fulls')
    return window.open(url, name, fulls);
}