import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import CacpUI from '@cacp/ui'

import 'normalize.css/normalize.css'
import 'element-plus/dist/index.css' //先引入element plus样式
import '@cacp/ui/dist/index.css' //后引入@cacp/ui样式
import './assets/main.less'

import App from './App.vue'
import router from './router'
import plugins from './plugins'
import directives from './directives'
import { pinia } from './stores'
import { addEventListener } from './utils/frame'
import { BridgeCore } from '@cacp/bridge-core'
import DictTag from '@/components/DictTag/dictTag.vue'
import { getDict } from '@/utils/dict'

import DynamicColumnDialog from '@/components/DynamicColumnSelector/DynamicColumnDialog.vue'

// 初始化通信实例
const bridge = new BridgeCore({
  allowedOrigins: [], //门户地址
  source: 'subApp'
})

const app = createApp(App)

// 注册全局字典属性
app.config.globalProperties.$dict = {
  get: getDict,
  type: async (dictType: string) => {
    return await getDict(dictType)
  }
}

// 声明全局属性类型
declare module 'vue' {
  interface ComponentCustomProperties {
    $dict: {
      get: (dictType: string) => Promise<any[]>
      type: (dictType: string) => Promise<any[]>
    }
  }
}
app.use(router)
app.use(plugins)
app.use(directives)
app.use(pinia)
app.use(DynamicColumnDialog)
app.component('DictTag', DictTag)
// 绑定message监听,addEventListener中调用了store，所以需要放在app.use(pinia)之后调用。
addEventListener(bridge)

app.use(ElementPlus, {
  locale: zhCn
})
app.use(CacpUI)

app.mount('#app')
