import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import '@unocss/reset/sanitize/sanitize.css'
import '@unocss/reset/sanitize/assets.css'
import 'virtual:uno.css'
import "vant/es/image-preview/style";
import { ImagePreview } from 'vant';
import Vconsole from 'vconsole'
/*
new Vconsole()*/
import router from "./router/index.js";
import i18n from './i18n'
import gsap from 'gsap'
import { ScrollTrigger, ScrollToPlugin } from 'gsap/all'

// 注册 GSAP 插件
gsap.registerPlugin(ScrollTrigger, ScrollToPlugin)

const app = createApp(App);
app.use(router);
app.use(ImagePreview);
app.use(i18n)

// 添加到全局属性中，方便在组件中使用
app.config.globalProperties.$gsap = gsap

app.directive('no-space', {
    mounted(el) {
        el.addEventListener('input', (e) => {
            const originalValue = e.target.value;
            const newValue = originalValue.replace(/\s/g, '');
            if (originalValue !== newValue) {
                e.target.value = newValue;
                e.target.dispatchEvent(new Event('input'));
            }
        });
    }
})
// 注册全局自定义指令
app.directive('decimal', {
    mounted(el) {
        el.addEventListener('input', (event) => {
            // 获取当前输入的值
            let value = event.target.value;

            // 匹配有效的数字（最多一位小数）
            const regex = /^\d*\.?\d{0,1}$/;

            if (!regex.test(value)) {
                // 如果不匹配，将输入值设置为最后一次有效输入的值
                event.target.value = el._lastValidValue || '';
            } else {
                // 如果匹配，保存该有效输入值
                el._lastValidValue = value;
            }
        });
    }
});
app.mount('#app');
