import App from '@/App'
import store from '@/store'
import appSettings from '@/core/config/app-settings'
import mpShareMixin from '@/platform/mp-share'
import Base64Codec from '@/core/infra/codec-base64'
import { registerHttpInterceptors, createHttpFacade } from '@/core/http'
import * as businessApi from '@/core/api'
import { buildRuntimeToolkit } from '@/core/kit/runtime-toolkit'
import { installGlobalShortcuts } from '@/features/global-shortcuts'
import { createAppI18n } from './i18n-factory'

// #ifdef H5
import { runningInsideWeChat } from '@/platform/h5-wechat'
// #endif

function attachSharedRuntime(host, i18n) {
	const sink = host.prototype || host
	sink.$store = store
	sink.$base64 = new Base64Codec()
	// #ifdef H5
	sink.$jssdk = { isWechat: runningInsideWeChat }
	// #endif
	sink.$appKit = buildRuntimeToolkit(i18n)
	sink.$http = createHttpFacade()
	sink.$api = businessApi
	installGlobalShortcuts(sink)
	registerHttpInterceptors(i18n)
}

// #ifndef VUE3
import Vue from 'vue'

export function bootstrapVue2App(RootApp = App) {
	const i18n = createAppI18n(Vue)
	Vue.mixin(mpShareMixin)
	Vue.config.productionTip = !!appSettings.debug
	attachSharedRuntime(Vue, i18n)
	RootApp.mpType = 'app'
	return new Vue({ i18n, store, ...RootApp }).$mount()
}
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'

export function bootstrapVue3App(RootApp = App) {
	const i18n = createAppI18n()
	const app = createSSRApp(RootApp)
	app.use(store)
	app.use(i18n)
	app.mixin(mpShareMixin)
	attachSharedRuntime(app.config.globalProperties, i18n)
	return { app }
}
// #endif
