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 DictSelection from '@/components/Dict/DictSelection';
import HeaderTitle from '@/components/header-title';
import authSelection from '@/components/authSelection';
import seekPage from '@/components/seekPage';
Vue.component('HeaderTitle', HeaderTitle);
Vue.component('authSelection', authSelection);
Vue.component('seekPage', seekPage);
import clickOnce from './utils/clickOnce.js';
// 注册全局自定义指令
Vue.directive('click-once', clickOnce);

// 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';
import fileMain from '@/components/addDoc/index.vue';
import {
  handleInputPublicHasPoint,
  handleInputPublic
} from './utils/inputHandler';

Vue.prototype.$handleInputPublicHasPoint = handleInputPublicHasPoint;
Vue.prototype.$handleInputPublic = handleInputPublic;

// // register globally
import '@/icons';
Vue.component('DictSelection', DictSelection);
Vue.config.productionTip = false;
Vue.component('fileMain', fileMain);

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);
let instance = null;

function render(props = {}) {
  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);
  props.onGlobalStateChange((state, prev) => {
    // state: 变更后的状态; prev 变更前的状态
    initParentStore(state);
  });

  render(props);
}

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