import { createSSRApp } from 'vue';
import App from './App.vue';
import pinia from './store';
import 'uno.css';
import { previewImage, selectDictLabel, selectDictLabels } from './utils/ruoyi';
import { useDict } from '@/utils/dict';
import uviewPlus, { setConfig } from 'uview-plus';
import { navigateBackOrHome, showToast } from '@/utils/common';
import routerGuard from '@/uni_modules/hh-router-guard/src/index';
import { useInfoStore } from '@/store';
const uviewProps: any = {
    config: {
        loadFontOnce: true,
        color: {
            primary: '#37A954',
        },
    },
    props: {
        form: {
            labelStyle: {
                color: '#333',
                fontSize: '30rpx',
            },
        },
        input: {
            color: '#333',
            fontSize: '30rpx',
            placeholderStyle: 'color: #ccc; font-weight: 400;fontSize:30rpx;',
        },
        textarea: {
            color: '#333',
            fontSize: '30rpx',
            placeholderStyle: 'color: #ccc; font-weight: 400;fontSize:30rpx;',
        },
        radio: {
            activeColor: '#37A954',
        },
        checkbox: {
            activeColor: '#37A954',
        },
    },
};
export function createApp() {
    const app = createSSRApp(App);
    app.use(pinia);
    // setConfig(uviewProps)
    app.use(uviewPlus, () => {
        return {
            options: {
                // 这里可以配置uView的全局属性
                ...uviewProps,
            },
        };
    });
    app.use(routerGuard, {
        // 白名单：无需登录即可访问的页面路径
        whiteList: [
            '/pages/login/login', // 登录页
            '/pages/plant/index',
            '/pages/switch/index1',
            '/tools/map-draw-area/index',
            '/plant/species/config/index',
            '/plant/base/gap-base-info/index',
            '/plant/base/base-edit/index',
        ],

        // 自定义登录检查函数（返回 true 表示已登录）
        checkLogin: () => {
            //检查token是否过期 true代表过期了
            const token = useInfoStore().isTokenExpired();
            return !token;
        },

        // 登录页面路径 === 需要替换为实际项目中登录页面的路径
        loginPath: '/pages/login/login',

        // 未登录时的处理逻辑
        loginHandler: (to: string) => {
            uni.navigateTo({
                url: `/pages/login/login?redirect=${encodeURIComponent(to)}`,
            });
        },
    });
    app.config.globalProperties.navigateBackOrHome = navigateBackOrHome;
    app.config.globalProperties.showToast = showToast;
    app.config.globalProperties.selectDictLabel = selectDictLabel;
    app.config.globalProperties.selectDictLabels = selectDictLabels;
    app.config.globalProperties.useDict = useDict;
    app.config.globalProperties.previewImage = previewImage;
    return {
        app,
        Pinia: { pinia },
    };
}
