import {createApp} from 'vue'
import router from "@/router/router.js"
import "@/assets/base.css"
import "@/lib/array.js"

// primeicons reference: https://primevue.org/icons/
import 'primeicons/primeicons.css'

// 安装tailwind: https://tailwindcss.tw/docs/guides/vite
//
// 安装postcss：https://tailwindcss.com/docs/installation/using-postcss
// 注意需要把帮助中的 tailwind.config.js 改名为 tailwind.config.cjs
//
// 使用文档：https://tailwindcss.tw/docs
import "@/assets/tailwind.css"

// import PrimeVue
import PrimeVue from 'primevue/config';

// directives & services
import Tooltip from "primevue/tooltip";
import Ripple from "primevue/ripple";
import Badge from "primevue/badge";

import BadgeDirective from "primevue/badgedirective";
import ToastService from "primevue/toastservice";
import ConfirmationService from "primevue/confirmationservice";
import DialogService from "primevue/dialogservice";

//  theme
import Aura from '@primevue/themes/aura';
import {definePreset} from "@primevue/themes";
import {colors} from "@/lib/theme.js"

let TPreset = definePreset(Aura, {
	semantic: {
		primary: {
			50: '{blue.50}',
			100: '{blue.100}',
			200: '{blue.200}',
			300: '{blue.300}',
			400: '{blue.400}',
			500: '{blue.500}',
			600: '{blue.600}',
			700: '{blue.700}',
			800: '{blue.800}',
			900: '{blue.900}',
			950: '{blue.950}'
		},
		colorScheme: {
			light: {
				primary: {
					color: '{primary.500}',
					contrastColor: '#ffffff',
					hoverColor: '{primary.600}',
					activeColor: '{primary.700}'
				}
			}
		}
	}
})

const theme = window.X_VIEW_DATA ? window.X_VIEW_DATA.teaTheme : null
window.X_VIEW_THEME = theme
if (typeof theme === "string" && theme.length > 0 && colors.$contains(theme)) {
	TPreset = definePreset(Aura, {
		semantic: {
			primary: {
				50: '{' + theme + '.50}',
				100: '{' + theme + '.100}',
				200: '{' + theme + '.200}',
				300: '{' + theme + '.300}',
				400: '{' + theme + '.400}',
				500: '{' + theme + '.500}',
				600: '{' + theme + '.600}',
				700: '{' + theme + '.700}',
				800: '{' + theme + '.800}',
				900: '{' + theme + '.900}',
				950: '{' + theme + '.950}'
			},
			colorScheme: {
				light: {
					primary: {
						color: '{primary.500}',
						contrastColor: '#ffffff',
						hoverColor: '{primary.600}',
						activeColor: '{primary.700}'
					}
				}
			}
		}
	})
}

/**
 *
 * @param {{ app:Object, useRouter:Boolean }} props
 */
const setupApp = props => {
	const app = createApp(props.app);

	// PrimeVue
	app.use(PrimeVue, {
		// https://primevue.org/configuration/#locale
		locale: {
			startsWith: "开头",
			contains: "包含",
			notContains: "不包含",
			endsWith: "结束",
			equals: "等于",
			notEquals: "不等于",
			noFilter: "没有过滤器",
			lt: "小于",
			lte: "小于等于",
			gt: "大于",
			gte: "大于等于",
			dateIs: "日期为",
			dateIsNot: "日期非",
			dateBefore: "日期之前",
			dateAfter: "日期之后",
			clear: "清除",
			apply: "应用",
			matchAll: "匹配所有",
			matchAny: "匹配任一个",
			addRule: "添加规则",
			removeRule: "删除规则",

			accept: "确定",
			reject: "拒绝",
			choose: "选择文件",
			upload: "上传",
			cancel: "取消",
			completed: "已完成",
			pending: "进行中",
			fileSizeTypes: ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],

			chooseYear: "选择年份",
			chooseMonth: "选择月份",
			chooseDate: "选择日期",
			prevDecade: "上十年",
			nextDecade: "下十年",
			prevYear: "上一年",
			nextYear: "下一年",
			prevMonth: "上一月",
			nextMonth: "下一月",
			monthNames: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
			monthNamesShort: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
			dayNames: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
			dayNamesShort: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
			dayNamesMin: ['日', '一', '二', '三', '四', '五', '六'],
			today: "今天",
			dateFormat: "yy-mm-dd",
			firstDayOfWeek: 0,
			showMonthAfterYear: true,
			weekHeader: " ",
			am: "上午",
			pm: "下午",
			prevHour: "上一小时",
			nextHour: "下一小时",
			prevMinute: "上一分钟",
			nextMinute: "下一分钟",
			prevSecond: "上一秒钟",
			nextSecond: "下一秒钟",

			weak: "弱",
			medium: "中",
			strong: "强",
			passwordPrompt: "请输入密码",
			searchMessage: "{0} 个结果",
			selectionMessage: "已选中 {0} 个",
			emptySelectionMessage: "没有选中的结果",
			emptySearchMessage: "没有匹配的结果",
			fileChosenMessage: "{0}个文件",
			noFileChosenMessage: "没有选择文件",
			emptyMessage: "没有可用的选项"
		},
		theme: {
			preset: TPreset,
			options: {
				darkModeSelector: '.dark',
			}
		}
	});

	// router
	if (props.useRouter !== false) {
		app.use(router)
	}

	// components
	app.component("Badge", Badge)

	// directives
	app.directive("tooltip", Tooltip)
	app.directive("badge", BadgeDirective)
	app.directive("ripple", Ripple)

	// services
	app.use(ToastService)
	app.use(ConfirmationService)
	app.use(DialogService)

	app.mount('#app')
}

export default setupApp