//import '@babel/polyfill';

// Vue
import Vue from 'vue';
import App from './App';

// 设置
import setting from './setting';

// 混合
import mixinApp from './mixins/app';
import mixinPage from './mixins/page';

// 插件
import plugins from './plugins';

// store
import store from './store';

// iView 和 iView Pro
import ViewUI from 'view-design';
import iViewPro from './libs/iview-pro/iview-pro.min.js';

// 菜单和路由
import router from './router';
import menuHeader from './menu/header';
import menuSider from './menu/sider';

// 多语言
import i18n from './i18n';

// 方法
import { getHeaderName, getMenuSider, getSiderSubmenu } from './libs/system';

// 组件
import Copyright from './components/copyright';
import Link from './components/link';
import RichEditor from './components/richEditor';
import DialogSelect from './components/select/dialogSelect';
import AttachmentUpload from './components/upload/attachment';
import ImageUpload from './components/upload/image';
import ViewForm from './components/view/view-form';
import ViewTable from './components/view/view-table';
import ModalForm from './components/view/modal-form';
import ModalTable from './components/view/modal-table';
import FormSetting from './components/view/form-setting';
import TableSetting from './components/view/table-setting';
import ModalFormFilter from './components/view/modal-form-filter';
import ModalFormSort from './components/view/modal-form-sort';

// API
import modelApi from './api/model';
import dataSourceApi from './api/dataSource';

// 使用样式，修改主题可以在 styles 目录下创建新的主题包并修改 iView 默认的 less 变量
// 参考 https://www.iviewui.com/docs/guide/theme
import './styles/index.less';
import './libs/iview-pro/iview-pro.css';

// fa图标
import '@fortawesome/fontawesome-free/css/all.min.css';

// CKEditor
import ckEditor from 'ckeditor4-vue';

// 指令
import focus from './directive/focus';
import tabHide from './directive/tabHide';

// 动态路由
import dynamicRouter from './router/dynamic';

// 基础布局
import basicLayout from './layouts/basic-layout';

// libs
import lodop from './libs/lodop';
import request from './libs/request';
import util from './libs/util';

const components = {
    Copyright,
    RichEditor,
    DialogSelect,
    AttachmentUpload,
    ImageUpload,
    ViewForm,
    ViewTable,
    ModalForm,
    ModalTable,
    FormSetting,
    TableSetting,
    ModalFormFilter,
    ModalFormSort,
    iLink: Link
};

const install = function(Vue, opts = {}) {
    if (install.installed) return;
    //locale.use(opts.locale);
    //locale.i18n(opts.i18n);

    if (window) window.$t = (key, value) => i18n.t(key, value);

    Vue.use(plugins);
    Vue.use(ViewUI, {
        i18n: (key, value) => i18n.t(key, value)
    });

    Vue.mixin(mixinApp);

    Vue.use(iViewPro);
    Vue.use(ckEditor);

    // 组件
    Object.keys(components).forEach(key => {
        Vue.component(key, components[key]);
    });

    // 指令，自动获得焦点
    Vue.directive('focus', focus);
    // 指令，隐藏Tab标签，保留内容
    Vue.directive('tab-hide', tabHide);

    // eCharts图表
    // Vue.prototype.$echarts = echarts;
};

// auto install
// if (typeof window !== 'undefined' && window.Vue) {
//     install(window.Vue);
// }

const API = {
    version: process.env.VERSION,
    // locale: locale.use,
    // i18n: locale.i18n,
    install
    //...components
};

const created = async (frameInRoutes, pages, layout) => {
    // 加载设置
    await store.dispatch('admin/setting/init');
    // 初始化动态路由
    let dynamicRoutes = await dynamicRouter.init(router, pages, layout);
    // 处理路由 得到每一级的路由设置
    store.commit('admin/page/init', [...frameInRoutes, ...dynamicRoutes]);
    // 设置顶栏菜单
    store.commit('admin/menu/setHeader', menuHeader);
    // 加载用户登录的数据
    store.dispatch('admin/account/load');
    // 加载边栏菜单
    store.dispatch('admin/menu/loadSider');
    // 初始化全屏监听
    store.dispatch('admin/layout/listenFullscreen');
};

const routeChanged = (to, from) => {
    setTimeout(() => {
        const path = to.path;
        let menu = menuSider;
        if (store.state.admin.setting.dynamicSiderMenu) {
            menu = store.getters['admin/menu/sider'];
        }

        const headerName = getHeaderName(path, menu);
        // 在 404 时，是没有 headerName 的
        if (headerName !== null) {
            store.commit('admin/menu/setHeaderName', headerName);

            const filterMenuSider = getMenuSider(menu, headerName);
            store.commit('admin/menu/setSider', filterMenuSider);
            store.commit('admin/menu/setActivePath', path);

            const openNames = getSiderSubmenu(path, menu);
            store.commit('admin/menu/setOpenNames', openNames);
        }

        //this.appRouteChange(to, from);
    });
};

export default API;

export { Vue, App, router, store, i18n, setting, basicLayout, lodop, request, mixinPage, modelApi, dataSourceApi, util, created, routeChanged };
