// PWA 安装状态
let deferredPrompt: any = null;

// 控制是否显示安装提示
let showInstallPrompt = true;

// 监听 beforeinstallprompt 事件
window.addEventListener('beforeinstallprompt', (e) => {
    if (!showInstallPrompt) {
        e.preventDefault();
        return;
    }
    deferredPrompt = e;
});

// 检查是否为 PWA 模式
export const isPwa = () => {
    return window.matchMedia('(display-mode: standalone)').matches ||
        (window.navigator as any).standalone ||
        document.referrer.includes('android-app://');
};

// 禁用 PWA 安装提示
export const disablePwaPrompt = () => {
    showInstallPrompt = false;
    if (deferredPrompt) {
        deferredPrompt = null;
    }
};

// 启用 PWA 安装提示
export const enablePwaPrompt = () => {
    showInstallPrompt = true;
};

// 手动触发 PWA 安装提示
export const showPwaInstallPrompt = async () => {
    if (!deferredPrompt) {
        console.log('No PWA installation prompt available');
        return;
    }
    deferredPrompt.prompt();
    const { outcome } = await deferredPrompt.userChoice;
    deferredPrompt = null;
    return outcome;
}; 