<script>
import { getToken } from '@/utils/auth'
import { initializeLocale, translateLiteral } from '@/locales'
import useUserStore from '@/store/modules/user'

let wrapped = false

function wrapUniTextApi() {
	if (wrapped) return
	wrapped = true

	const rawShowToast = uni.showToast
	const rawShowModal = uni.showModal
	const rawShowLoading = uni.showLoading

	uni.showToast = (options = {}) => {
		const next = { ...options }
		if (typeof next.title === 'string') {
			next.title = translateLiteral(next.title)
		}
		return rawShowToast(next)
	}
	uni.showLoading = (options = {}) => {
		const next = { ...options }
		if (typeof next.title === 'string') {
			next.title = translateLiteral(next.title)
		}
		return rawShowLoading(next)
	}
	uni.showModal = (options = {}) => {
		const next = { ...options }
		if (typeof next.title === 'string') {
			next.title = translateLiteral(next.title)
		}
		if (typeof next.content === 'string') {
			next.content = translateLiteral(next.content)
		}
		if (typeof next.confirmText === 'string') {
			next.confirmText = translateLiteral(next.confirmText)
		}
		if (typeof next.cancelText === 'string') {
			next.cancelText = translateLiteral(next.cancelText)
		}
		return rawShowModal(next)
	}
}

export default {
	onLaunch: function () {
		initializeLocale()
		wrapUniTextApi()
		if (getToken()) {
			useUserStore().getInfo().catch(() => {})
		}
	},
	onShow: function () {
		initializeLocale()
		wrapUniTextApi()
	},
	onHide: function () {
	}
}
</script>

<style lang="scss">
@import "uview-plus/index.scss";
@import '@/static/scss/index.scss';
</style>
