import { createApp } from 'vue';
import App from '@/App.vue';
import axios from 'axios';
const app = createApp(App);
// 设置 axios 基础 URL
axios.defaults.baseURL = import.meta.env.VITE_API_URL;
// 将 axios 实例添加到 Vue 的全局属性中，使其在所有组件中可通过 this.$axios 访问
app.config.globalProperties.$axios = axios;
//vant
import { Tabbar, TabbarItem } from 'vant';
app.use(Tabbar);
app.use(TabbarItem);
// pinia store
import { createPinia } from 'pinia';
const pinia = createPinia();
app.use(pinia);

import router from '@/router';
app.use(router);

// main app css
import '@/assets/css/app.css';

/* sidebar */
import '@/assets/css/sidebar.css';
// perfect scrollbar
import PerfectScrollbar from 'vue3-perfect-scrollbar';
app.use(PerfectScrollbar);

//vue-meta
import { createHead } from '@vueuse/head';
const head = createHead();
app.use(head);

// set default settings
import appSetting from '@/app-setting';
appSetting.init();

//vue-i18n
import i18n from '@/i18n';
app.use(i18n);

// tippy tooltips
import { TippyPlugin } from 'tippy.vue';
app.use(TippyPlugin);

//input mask
import Maska from 'maska';
app.use(Maska);

//markdown editor
import VueEasymde from 'vue3-easymde';
import 'easymde/dist/easymde.min.css';
app.use(VueEasymde);

// popper
import Popper from 'vue3-popper';
app.component('Popper', Popper);

// json to excel
import vue3JsonExcel from 'vue3-json-excel';
app.use(vue3JsonExcel);

/* 引入element-plus */
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

app.use(ElementPlus)

/* 全局引入 */
import './permission'


import store from './stores';
app.use(store);
// 如果您正在使用CDN引入，请删除下面一行。
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

app.config.globalProperties.checkPermission = (value: any): boolean => {
    if (value && Array.isArray(value) && value.length > 0) {
        const permissions = store.getters.routersAlls;
        const permissionDatas = value;
        const all_permission = '*:*:*';
        const hasPermission = permissions.some((permission: string) => {
            return all_permission === permission || permissionDatas.includes(permission);
        });
        return !!hasPermission;
    } else {
        return false;
    }
};

/* 全局注册表格及搜索组件 */
import globalComponents from '../src/composables/global-components'; // 引入全局组件插件
app.use(globalComponents); // 使用全局组件插件

app.config.globalProperties.checkPermission = (value: string[]): boolean => {
    if (value && Array.isArray(value) && value.length > 0) {
        const permissions = store.getters.routersAlls;
        const permissionDatas = value;
        const all_permission = '*:*:*';
        const hasPermission = permissions.some((permission: string) => {
            return all_permission === permission || permissionDatas.includes(permission);
        });
        return !!hasPermission;
    } else {
        return false;
    }
};
app.mount('#app');
