import meta from './updater_meta.json'
import { CapacitorUpdater } from '@capgo/capacitor-updater'
import { Capacitor } from '@capacitor/core'
import axios from 'axios'
if (Capacitor.isNativePlatform()) {
    CapacitorUpdater.notifyAppReady()
}
window.packageVersion = meta.version
console.log('meta', meta)

CapacitorUpdater.addListener('download', ({ percent }) => {
    console.log('download progress:', percent)
})
CapacitorUpdater.addListener('downloadFailed', e => {
    console.log('downloadFailed', e)
})

const checkUpdate = async (showPrompt = false) => {
    const { data: remoteMeta } = await axios.get('https://zm-shorts.oss-cn-hangzhou.aliyuncs.com/packages/meta.json', {
        withCredentials: false
    })
    console.log('remoteMeta', remoteMeta)
    if (remoteMeta.version > meta.version) {
        const version = await CapacitorUpdater.download({
            url: remoteMeta.url,
            version: remoteMeta.version + ''
        })
        console.log('version', version)
        CapacitorUpdater.set(version)
    }
}

export { checkUpdate }
