import { defaultPrintConfig } from './config/config';
import { generateUUID, getAppUUID, setAppUUID } from './utils/uuid';

// app.ts
App<IAppOption>({
    globalData: {
        env: 'release',
        uuid: '',
        printConfig: defaultPrintConfig,
    },
    onLaunch() {
        wx.cloud.init({
            env: 'cloud1-9gifs7a2756e2c87',
            traceUser: true,
        });

        const accountInfo = wx.getAccountInfoSync();
        const env = accountInfo.miniProgram.envVersion || 'release';
        /**
         * 设置环境变量
         * 开发环境：develop
         * 体验环境：trial
         * 正式环境：release
         */
        this.globalData.env = env;

        // 从 localStorage 读取 UUID，如果没有则生成并存储
        let uuid = getAppUUID();
        if (!uuid) {
            uuid = generateUUID();
            setAppUUID(uuid);
            console.log('生成并存储 UUID', uuid);
        }
        this.globalData.uuid = uuid;

        if (env !== 'release') {
            const printConfig =
                wx.getStorageSync('printConfig') || defaultPrintConfig;
            this.globalData.printConfig = printConfig;
        }
    },

    onShow() {
        // 小程序显示时的生命周期
    },

    onHide() {
        // 小程序隐藏时的生命周期
    },

    getPrintConfig(): PrintConfig {
        return this.globalData.printConfig || defaultPrintConfig;
    },

    setPrintConfig(config: PrintConfig) {
        this.globalData.printConfig = config;
        wx.setStorageSync('printConfig', config);
    },
});
