import meta from "@/assets/meta.json"
import { CapacitorUpdater } from "@capgo/capacitor-updater"
import { Capacitor } from "@capacitor/core"
import axios from "axios"
if (Capacitor.isNativePlatform()) {
    CapacitorUpdater.notifyAppReady()
}
window.meta = meta
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(
        import.meta.env.VITE_UPDATE_META_URL,
        {
            withCredentials: false,
            headers: {
                "Cache-Control": "no-cache"
            }
        }
    )
    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 }
