import { App, Component, createApp } from 'vue';
import MobVue3Components, {
  i18n,
  loadingDirective,
  piniaInstance,
} from '@ibiz-template/mob-vue3-components';
import Vant, { allowMultipleToast } from 'vant';
import UserRegister from './user-register';

// 允许同时存在多个 toast
allowMultipleToast();

/**
 * 创建 vue3 实例，避免多实例情况下全局方法未成功挂载
 *
 * @author chitanda
 * @date 2022-12-29 11:12:25
 * @export
 * @param {Component} rootComponent
 * @param {IData} [rootProps]
 * @return {*}  {Promise<App<Element>>}
 */
export function createVueApp(
  rootComponent: Component,
  rootProps?: IData,
): App<Element> {
  const app = createApp(rootComponent, rootProps);
  // 全局 Vue 异常处理
  app.config.errorHandler = function (err: unknown): void {
    ibiz.util.error.handle(err);
  };
  app.use(i18n);
  app.use(Vant).use(piniaInstance).use(MobVue3Components).use(UserRegister);

  ibiz.plugin.register(app);
  // loading指令
  app.directive('loading', loadingDirective);
  return app;
}
