import Vue from 'vue';
import './public-path';
import initParentStore from '../qiankun_config/store';
import App from './App.vue';
import store from './store';
import router from './router';
import permission from './utils/permission';
import { MAP_KEY, LICENSE_CODE } from '@/config/setting';
import EleAdmin from 'ele-admin';
import VueClipboard from 'vue-clipboard2';
import i18n from './i18n';
import './styles/index.scss';
import '@/components/FormGenerator/styles/index.scss';
import DictSelection from '@/components/Dict/DictSelection';
// import tinymce from '@/components/FormGenerator/components/tinymce/index.js';
// import TinymceEditor from '@/components/TinymceEditor/index.vue';
// import SvgIcon from '@/components/FormGenerator/components/SvgIcon'; // svg component
import HeaderTitle from '@/components/header-title';
Vue.component('HeaderTitle', HeaderTitle);

import { create, all } from 'mathjs'; // 引入mathjs工具并初始化
const config = {
  number: 'BigNumber',
  precision: 64
};
Vue.prototype.$math = create(all, config);
// // register globally
// Vue.component('tinymce', TinymceEditor);
import '@/icons';

// bpmnProcessDesigner 需要引入
import MyPD from '@/components/bpmnProcessDesigner/package/index.js';
Vue.use(MyPD);
import '@/components/bpmnProcessDesigner/package/theme/index.scss';
import 'bpmn-js/dist/assets/diagram-js.css';
import 'bpmn-js/dist/assets/bpmn-font/css/bpmn.css';
import 'bpmn-js/dist/assets/bpmn-font/css/bpmn-codes.css';
import 'bpmn-js/dist/assets/bpmn-font/css/bpmn-embedded.css';

Vue.component('DictSelection', DictSelection);
Vue.config.productionTip = false;

// 表单重置
Vue.prototype.resetForm = function resetForm(refName) {
  if (this.$refs[refName]) {
    this.$refs[refName].resetFields();
  }
};

Vue.use(EleAdmin, {
  response: {
    dataName: 'list'
  },
  mapKey: MAP_KEY,
  license: LICENSE_CODE,
  i18n: (key, value) => i18n.t(key, value)
});
Vue.use(permission);
Vue.use(VueClipboard);
// new Vue({
//   router,
//   store,
//   i18n,
//   render: (h) => h(App)
// }).$mount('#app');

let instance = null;

function render(props = {}) {
  console.log(router, 2222);
  const { container, routerBase } = props;
  // const router = new VueRouter({
  //   base: window.__POWERED_BY_QIANKUN__ ? routerBase : process.env.BASE_URL,
  //   mode: 'history',
  //   routes
  // });

  instance = new Vue({
    router,
    store,
    i18n,
    render: (h) => h(App)
  }).$mount(container ? container.querySelector('#app') : '#app');
}

if (!window.__POWERED_BY_QIANKUN__) {
  // 这里是子应用独立运行的环境，实现子应用的登录逻辑
  render();
}

export async function bootstrap() {
  console.log('[vue] vue app bootstraped');
}

export async function mount(props) {
  console.log('[vue] props from main framework', props);
  Vue.prototype.$portalStore = props.store;
  initParentStore(props.store);
  // initParentStore(props.store?.state);
  props.onGlobalStateChange((state, prev) => {
    // state: 变更后的状态; prev 变更前的状态
    initParentStore(state);
  });

  render(props);
}

export async function unmount() {
  instance.$destroy();
  instance.$el.innerHTML = '';
  instance = null;
}
