import '@/utils/public-path';
import Vue from 'vue';
import App from './App';
import router from './router';
import store from './store';
import { axios } from './service/api/index';
import directives from './directives';
import limit from '@/utils/limiting';
import scrollToError from '@/assets/common.js';

Vue.config.productionTip = false;

if (process.env.NODE_ENV == 'development') { // eslint-disable-line
  Vue.config.devtools = true;
} else {
  Vue.config.devtools = false;
}
Vue.prototype.scrollToError = scrollToError;
Vue.prototype.axios = axios;
Vue.prototype.axios.withCredentials = true;
Vue.prototype.axios.defaults.timeout = 50000;
window.ELEMENT.Dialog.props.closeOnClickModal.default = false;
Object.keys(directives).map(item => Vue.directive(item, directives[item]));
window.$bus = new Vue();
let flag = false;

Vue.prototype.$tips = function({ message = '提示', type = 'success' }) {
  if (flag) {
    return;
  } else {
    this.$message({ message, type });
  }
  flag = true;
  setTimeout(_ => {
    flag = false;
  }, 1000);
};

let instance = null;
function render(props = {}) {
  const { container } = props;
  instance = new Vue({
    router,
    store,
    render: h => h(App)
  }).$mount(container ? container.querySelector('#app') : '#app');
}
// 独立运行时
if (!window.__POWERED_BY_QIANKUN__) {
  console.log('独立运行');
  limit({
    createApp() {
      window.getLimit(router, 'marketing', '/marketing/').then(() => {
        render();
      });
    }
  });
}

export async function bootstrap() {
  console.log('[vue] vue app bootstraped');
}
export async function mount(props) {
  limit({
    createApp() {
      window.getLimit(router, 'marketing', '/marketing/').then(() => {
        render();
      });
    }
  });
}
export async function unmount() {
  instance.$destroy();
  instance.$el.innerHTML = '';
  instance = null;
}

/* eslint-disable no-new */
