import { createApp } from './main'
const { app, router, store } = createApp()
if(window.__INITIAL_STATE__){
    store.replaceState(window.__INITIAL_STATE__)
}
// router.beforeEach((to,from,next)=>{
//     const uskyDb = new indexedDb("uskyDb") //创建或连接DB数据库
//     uskyDb.openStore("menu","id",['list']).then((res:any)=>{
//         next()
//     })
// })
router.isReady().then(() => {
    //比较to、from不同时执行数据预取
    router.beforeResolve((to,from,next)=>{
        let toComponents = router.resolve(to).matched.flatMap(record=>Object.values(record.components))
        let fromComponents = router.resolve(from).matched.flatMap(record=>Object.values(record.components))
        let actived = toComponents.filter((c,i)=>{
            return fromComponents[i] !==c
        })
        if(!actived.length){
            return next()
        }
        //对所有匹配的路由组件调用`asyncData()
        Promise.all(actived.map((Component:any) =>{
            if(Component.asyncData){
                return Component.asyncData({
                    store,
                    route: router.currentRoute
                })
            }
        })).then(()=>{
            next()
        })
    })
    //页面刷新
    const matchedComponents = router.currentRoute.value.matched.flatMap(record =>Object.values(record.components))
    //对所有匹配的路由组件调用`asyncData()
    Promise.all(matchedComponents.map((Component:any) =>{
        if(Component.asyncData){
            return Component.asyncData({
                store,
                route: router.currentRoute
            })
        }
    }))
    
    app.mount('#app')
})