/*
 * @Author: Haijun Zhang
 * @Date: 2022-11-10 10:46:38
 * @LastEditTime: 2022-11-17 18:37:12
 * @LastEditors: Haijun Zhang
 * @Description:
 * @FilePath: \main-web-qiankun\src\core\register.ts
 */
import { registerMicroApps, start, addGlobalUncaughtErrorHandler } from 'qiankun'
import NProgress from 'nprogress'
import store from '@/store'
import 'nprogress/nprogress.css'
import './actions'
import { getMicroApp } from './config'

const startApp = async () => {
  const apps = await getMicroApp()
  registerMicroApps(apps, {
    beforeLoad: [
      () => {
        // 解决ros-plugin报错问题
        // if(window.Vue2){
        //   window.Vue = window.Vue2;
        //   delete window.Vue2
        // }else if(app.name === 'ros-plugin'){
        //   window.Vue2 = window.Vue;
        //   delete window.Vue;
        // }
        NProgress.start()
        store.commit('SET_APP_LOADING', true)
        // console.log('[LifeCycle] before load %c%s', 'color: green;', app.name)
        return Promise.resolve()
      }
    ],
    afterMount: [
      () => {
        NProgress.done()
        store.commit('SET_APP_LOADING', false)
        // console.log('[LifeCycle] after mount %c%s', 'color: green;', app.name)
        return Promise.resolve()
      }
    ],
    afterUnmount: [
      () => {
        // console.log('[LifeCycle] after unmount %c%s', 'color: green;', app.name)
        return Promise.resolve()
      }
    ]
  })
  // const prefetchList = apps.filter(item => item.name.includes('-web')).map(item => item.name)
  start({
    prefetch: 'all'
    // excludeAssetFilter(assetUrl:string){
    // 插件的静态资源不被qiankun劫持处理
    // return assetUrl.includes('/web-common-resource/')
    // }
  })
}
/**
 * 添加全局的未捕获异常处理器
 */
addGlobalUncaughtErrorHandler((event: Event | string) => {
  console.error(event)
  const { message: msg } = event as any
  // 加载失败时提示
  if (msg && msg.includes('died in status LOADING_SOURCE_CODE')) {
    console.error('微应用加载失败，请检查应用是否可运行')
  }
})
export default startApp
