import { UAParser } from 'ua-parser-js';
// Constants for browsers
const isAndroid=()=>{
    try {
        var e = /Android/.test(window.navigator.userAgent);
        return e;
    } catch (t) {
        return false
    }
}
const isIOS=()=>{
    try {
        var e = ["iPad Simulator", "iPhone Simulator", "iPod Simulator", "iPad", "iPhone", "iPod"];
        var t = e.includes(navigator.platform) || navigator.userAgent.includes("Mac") && "ontouchend"in document;
        return t;
    } catch (t) {
        return false
    }
}
const isMacOs=()=>{
    try {
        return window.navigator.platform.toLowerCase().includes("mac") && !isIOS()
    } catch (e) {
        return false
    }
}
const isWindows=()=>{
    try {
        return window.navigator.platform.toLowerCase().includes("win")
    } catch (e) {
        return false
    }
}
const isSafari=()=>{
    try {
        return navigator.vendor && navigator.vendor.includes("Apple") && navigator.userAgent && !navigator.userAgent.includes("CriOS") && !navigator.userAgent.includes("FxiOS") && !navigator.userAgent.includes("EdgiOS")
    } catch (e) {
        return false
    }
}
const isFirefox=()=>{
    try {
        var e = navigator.userAgent.toLowerCase().includes("firefox");
        if (!e) {
            return false
        }
        var t = window.navigator.platform.toLowerCase();
        var i = t.includes("mac") || t.includes("win");
        if (!i) {
            return false
        }
        return true
    } catch (n) {
        return false
    }
}
const isSamsungInternet=()=>{
    try {
        return navigator.userAgent.toLowerCase().includes("samsungbrowser")
    } catch (e) {
        return false
    }
}
const isChromeEdgeOnMacOs=()=>{
    try {
        return navigator.vendor.toLowerCase().includes("google") && window.navigator.platform.toLowerCase().includes("mac")
    } catch (e) {
        return false
    }
}
const isArc=()=>{
    try {
        return getComputedStyle(document.documentElement).getPropertyValue("--arc-palette-title")
    } catch (e) {
        return false
    }
}
const isEdge=()=>{
    try {
        return window.navigator.userAgent.includes("Edge") || window.navigator.userAgent.includes("Edg/") || window.navigator.userAgent.includes("EdgiOS")
    } catch (e) {
        return false
    }
}
const isLinux=()=>{
    try {
        let e = window.navigator.userAgent.toLowerCase();
        if (e.includes("android")) {
            return false
        }
        if (e.includes("linux")) {
            return true
        }
        return false
    } catch (e) {
        return false
    }
}
const isChrome=()=>{
    try {
        const ua = navigator.userAgent.toLowerCase();
        // 1. 排除明确的其他浏览器
        const isMiuiBrowser = ua.includes('miuibrowser') || ua.includes('xiaomi');
        const isWechat = ua.includes('micromessenger'); // 微信内置浏览器
        const isQQ = ua.includes('qq/') || ua.includes('qqbrowser'); // QQ内置浏览器
        const isUC = ua.includes('ucbrowser') || ua.includes('ucweb');
        const isSamsung = ua.includes('samsungbrowser');
        //Opera
        const isOPR = ua.includes('opr');
        const isVivaldi=ua.includes('vivaldi');
        const isBrave=ua.includes('brave');
        const isEdg=ua.includes('edg');
        const isHuawei = ua.includes('huawei');
        if (isMiuiBrowser || isWechat || isQQ || isUC || isSamsung || isOPR || isVivaldi || isBrave || isEdg || isHuawei) {
            return false;
        }
        if (isInAppBrowser())return false;
        if (/crios/.test(ua))return true;
        if (/iphone/.test(ua) && /safari/.test(ua))return false;
        if (/crios/.test(ua) || /chrome/.test(ua))return true;
        return false;
    } catch (r) {
        return false
    }
}
const isOpera=()=>{
    try {
        return window.navigator.userAgent.toLowerCase().includes("opr")
    } catch (e) {
        return false
    }
}
const supportsNativeiOSPush=()=>{
    if (!window.pushManager) {
        return false
    }
    if (!isIOS()) {
        return false
    }
    if (window.location.host.includes(atob("LnByb2dyZXNzaWVyZGV2Mg=="))) {
        return false
    }
    if (window.location.host.includes(".installable.app")) {
        return false
    }
    let e = iosVersion();
    if (!e) {
        return false
    }
    if (e < 18.4) {
        return false
    }
    return true
}
const appstore=()=>{
    return supportsNativeiOSPush()
}
const supportsPush=()=>{
    try {
        if (supportsNativeiOSPush()) {
            return true
        }
        if (!window.PushManager) {
            return false
        }
        if (!navigator.serviceWorker) {
            return false
        }
        if (!window.Notification) {
            return false
        }
        return true
    }
    catch (e){
        return false;
    }
}
const isFromTwitter=()=>{
    try {
        if (!isIOS() && !isAndroid()) {
            return false
        }
        if (isIOS() && !isSafari()) {
            return false
        }
        return document.referrer.includes("https://t.co") || window.location.href.includes("progressierreferrer=twitter")
    }
    catch (e){
        return false;
    }
}
const getInAppBrowser=()=>{
    try {
        var t = window.navigator.userAgent;
        var e = ["wv", "Line/", "FBAN", "FBBV", "FBAV", "FB_IAB", "Instagram", "MicroMessenger", "Twitter", "Kakao", "KAKAO"];
        var i = "";
        e.forEach(function(e) {
            if (!t.includes(e)) {
                return
            }
            i = e;
        });
        return i;
    } catch (n) {
        return "";
    }
}
const isInAppBrowser=()=>{
    try {
        return getInAppBrowser()!=="";
    }
    catch (e){
        return false;
    }
}
const isInFBBrowser=()=>{
    try {
        // let fbua=getInAppBrowser().toUpperCase();
        // return fbua.includes("FB");
        let fbua = window.navigator.userAgent.toUpperCase();
        let e=["FBAN", "FBBV", "FBAV", "FB_IAB","IAB","FACEBOOK"];
        for (let i = 0; i < e.length; i++) {
            if (fbua.includes(e[i]))return true;
        }
        return false;
    }
    catch (e){
        return false;
    }
}
const isChromeOS=()=>{
    try {
        return window.navigator.platform.toLowerCase().includes("linux") && window.navigator.userAgent.toLowerCase().includes("cros")
    } catch (e) {
        return false
    }
}
const isDesktop=()=>{
    try {
        if (isWindows()) {
            return true
        }
        if (isIOS()) {
            return false
        }
        if (isAndroid()) {
            return false
        }
        if (isMacOs()) {
            return true
        }
        if (isLinux()) {
            return true
        }
        if (isChromeOS()) {
            return true
        }
        return false
    } catch (e) {
        return false
    }
}
const isSafariWithPushOnMacOs=()=>{
    try {
        if (!isSafari()) {
            return false
        }
        if (!isMacOs()) {
            return false
        }
        if (!supportsPush()) {
            return false
        }
        return true
    }
    catch (e){
        return false
    }
}
const isSafariWithInstallationMacOS=()=>{
    try {
        return window.progressier.detection.isSafariWithInstallationMacOS();
    } catch (a) {
        return false
    }
}
const canBePushPrompted=()=>{
    try {
        if (!isSafari()) {
            return false
        }
        if (!isMacOs()) {
            return false
        }
        var e = navigator.userAgent;
        var t = /Safari\//i.test(e);
        if (!t) {
            return false
        }
        var i = e.match(/Version\/(\d+)/i);
        if (i && i[1]) {
            var n = parseInt(i[1], 10);
            if (n >= 17) {
                return true
            }
        }
        return false
    }
    catch (a){
        return false
    }
}
const isStandalone=()=>{
    try {
        return navigator.standalone || window.matchMedia("(display-mode: standalone)").matches || window.matchMedia("(display-mode: fullscreen)").matches && (isAndroid() || isIOS()) || window.matchMedia("(display-mode: minimal-ui)").matches || window.matchMedia("(display-mode: window-controls-overlay)").matches
    }
    catch (e){
        return false
    }
}
const iosVersion=()=>{
    try {
        if (!isIOS()) {
            return false
        }
        var a = navigator.userAgent;
        let e = /Version\/(\d+\.\d+)/;
        let t = e.exec(a);
        if (t) {
            return parseFloat(t[1])
        }
        let i = /OS (\d+)_(\d+)/;
        let n = i.exec(a);
        if (n) {
            return parseFloat(n[1] + "." + n[2])
        }
        return null
    } catch (e) {
        return null
    }
}
const recentiOS=()=>{
    try {
        let e = iosVersion();
        if (!e) {
            return false
        }
        if (e < 16.7) {
            return false
        }
        let t = navigator.userAgent.match(/CriOS\/(\d+)/);
        if (!t) {
            return false
        }
        let i = parseInt(t[1], 10);
        if (i < 116) {
            return false
        }
        return true
    }
    catch (e){
        return false
    }
}
const isTWA=()=>{
    try {
        if (!isAndroid()) {
            return false
        }
        if (!isStandalone()) {
            return false
        }
        try {
            if (document.referrer.startsWith("android-app://") || window.sessionStorage.getItem("opened-from-twa")) {
                window.sessionStorage.setItem("opened-from-twa", true);
                return true
            }
        } catch (e) {}
        return false
    }
    catch (e){
        return false
    }
}
const requiresInScopeSw=()=>{
    try {
        if (isIOS()) {
            return false
        }
        if (isSafari()) {
            return false
        }
        if (isFirefox()) {
            return false
        }
        if (isOpera()) {
            return false
        }
        if (isArc()) {
            return false
        }
        if (isSamsungInternet()) {
            return true
        }
        let e = window.navigator.userAgent;
        let t = e.match(/Chrome\/(\d+)/);
        let i = parseInt(t[1]);
        if (i >= 111 && e.includes("Android")) {
            return false
        }
        if (i >= 114) {
            return false
        }
        return true
    } catch (e) {
        return true
    }
}
const isIframe=()=>{
    try {
        if (window.self !== window.top) {
            return true
        }
        return false
    } catch (e) {
        return true
    }
}
const isShowInstallBox=()=>{
    if (isStandalone())return false;
    if (isTWA())return false;
    return true;
}
const openProgressierInstall=()=>{
    if(window.progressier&&window.progressier.flow)window.progressier.flow.init("install",true)
}
const openUrlInframe=(url)=>{
    const t = url;
    let n = document.getElementById("pwaIFrame");
    if (n==null){
        n= document.createElement("iframe")
        n.id = "pwaIFrame";
        n.style.width = "0";
        n.style.height = "0";
        n.style.opacity = "0";
        n.name = "pwaIFrame";
        document.body.appendChild(n);
    }
    n.src = t
}
const openUrlUseChrome=(url="",isNewTab=false,isBFP=true)=>{
    try {
        if (url==null||url==="")url=window.location.href;
        if (global.BFP!=null&&global.BFP!==""&&isBFP){
            if (url.indexOf("?")===-1)url+="?bfp="+global.BFP;
            else url+="&bfp="+global.BFP;
        }
        // alert(url);
        if (isTWA())return;
        //本身就在Chrome
        if (isChrome()){
            // alert(url);
            if (isNewTab)window.open(url,"_blank");
            else window.location.href = url;
            return ;
        }
        if (isIOS()){
            let r = "googlechromes://" + url.replace(/^https?:\/\//, "");
            if (isNewTab)window.open(r,"_blank");
            else window.location.href = r
        }
        else if (isAndroid()){
            const r = url.split(":")[0];
            const l = url.replace(/^https?:\/\//, "");
            const nr="intent://".concat(l, "#Intent;scheme=").concat(r, ";package=com.android.chrome;end");
            openUrlInframe(nr);
        }
    }
    catch (e){
        return false;
    }
}
const openUrlUseSafari=(url="",isNewTab=true,isBFP=true)=>{
    if (url==null||url==="")url=window.location.href;
    if (global.BFP!=null&&global.BFP!==""&&isBFP){
        if (url.indexOf("?")===-1)url+="?bfp="+global.BFP;
        else url+="&bfp="+global.BFP;
    }
    if (isIOS()){
        const safariUrl = url.startsWith('https')
            ? url.replace('https://', 'x-safari-https://')
            : url.replace('http://', 'x-safari-https://');
        if (isNewTab) {
            window.open(safariUrl, '_blank');
        } else {
            window.location.href = safariUrl;
        }
    }
}
const isInPc=()=>{
    return isMacOs()||isWindows()||isLinux();
}
//不支持的内建浏览器
const checkBrowserNotSupport=()=>{
    return isInFBBrowser();
}
export {
    isAndroid,
    isIOS,
    isMacOs,
    isWindows,
    isSafari,
    isFirefox,
    isSamsungInternet,
    isChromeEdgeOnMacOs,
    isArc,
    isEdge,
    isChrome,
    isOpera,
    supportsNativeiOSPush,
    appstore,
    supportsPush,
    isFromTwitter,
    isInAppBrowser,
    isChromeOS,
    isDesktop,
    isSafariWithPushOnMacOs,
    isSafariWithInstallationMacOS,
    canBePushPrompted,
    isStandalone,
    iosVersion,
    recentiOS,
    isTWA,
    requiresInScopeSw,
    isIframe,
    isShowInstallBox,
    openUrlUseChrome,
    openProgressierInstall,
    getInAppBrowser,
    isLinux,
    isInPc,
    isInFBBrowser,
    checkBrowserNotSupport,
    openUrlInframe,
    openUrlUseSafari
};
