<%= importor.lifecycle? `import lifeCycle from '${wedaRootRelativePath}lowcode/lifecycle'` : "const lifeCycle = {}" %>
import { app, $app } from '<%= wedaRootRelativePath %>app/weapps-api';
// 引入数据源管理器并进行初始化
import {
  EXTRA_API,
  createStateDataSourceVar,
  generateParamsParser,
} from '<%= wedaRootRelativePath %>datasources/index';

App({
  _query: {},
  globaldata: {
    _beforeCustomLaunchPromise: null,
  },
  async beforeCustomLaunch(query) {
    if (!this.globaldata?._beforeCustomLaunchPromise) {
      if (!this.globaldata) {
        this.globaldata = {};
      }
      this.globaldata._beforeCustomLaunchPromise = new Promise(async (resolve) => {
        try {
          EXTRA_API.setParams('$global', query || {});
          await createStateDataSourceVar('$global', generateParamsParser({ app, $page: {}, $w: app.__internal__?.$w }));
        } catch (e) {
          throw e;
        } finally {
          for (const queryId in app.dataset?.query || {}) {
            if (app.dataset.query[queryId]?._schema?.trigger === 'auto') {
              /**
               * query 初始化不阻塞生命周期
               */
              Promise.resolve()
                .then(async () => {
                  await app.dataset?.query?.[queryId]?.initPromise;
                  return app.dataset?.query?.[queryId]?.trigger?.();
                })
                .catch((e) => {
                  console.error(`query ${queryId} 初始化失败：`, e);
                });
            }
          }
          resolve();
        }
      });
    }
    return this.globaldata._beforeCustomLaunchPromise;
  },
  async onLaunch(options) {
    if (wx.onLazyLoadError) {
      wx.onLazyLoadError(({ type, subpackage, errMsg}) => {
        console.log('subpackage')
        if(type === 'subpackage' && subpackage?.find(item=>item === '<%= materialSubPackagePath %>')) {
          wx.showModal({
            title: '组件包加载失败',
            content: `加载组件包超时：${errMsg}，请退出重新进入。`,
            showCancel: false,
            success (res) {
              return
            }
          })

        }
      })
    }
    this.app = app;
    const onLaunch = lifeCycle.onLaunch || lifeCycle.onAppLaunch;
    let { query = {} } = options;
    this._query = query;

    await this.beforeCustomLaunch(query);
    onLaunch && onLaunch.call(this, options);
    // 初始私有全局数据
    this.$$global = {
      homePageId: '<%= appConfig.homePageId %>',
    };
  },
  async onShow(options) {
    await this.beforeCustomLaunch(this._query);
    const fn = lifeCycle.onShow || lifeCycle.onAppShow;
    fn && fn.call(this, options);
  },
  onHide() {
    const fn = lifeCycle.onHide || lifeCycle.onAppHide;
    fn && fn.call(this);
  },
  onError(msg) {
    const fn = lifeCycle.onError || lifeCycle.onAppError;
    fn && fn.call(this, msg);
  },
  onPageNotFound() {
    const fn = lifeCycle.onPageNotFound || lifeCycle.onAppPageNotFound;
    fn && fn.call(this);
  },
  onUnhandledRejection() {
    const fn = lifeCycle.onUnhandledRejection || lifeCycle.onAppUnhandledRejection;
    fn && fn.call(this);
  },
});
