// directives/numberDirective.js
export const numberDirective = {
    mounted(el, binding) {
        const { value = {} } = binding;
        const { allowDecimal = false, allowNegative = false } = value;
        let isComposing = false;

        // 查找 el-input 下的 input 元素
        const inputElement = el.querySelector('input');

        const handleInput = (e) => {
            if (isComposing) {
                return;
            }
            let input = inputElement.value;
            
            // 新处理逻辑：分步骤处理
            let processed = input.replace(/[^-\d.]/g, ''); // 第一步：过滤非法字符
            
            // 处理负号
            if (allowNegative) {
                const hasNegative = processed.includes('-');
                processed = processed.replace(/-/g, '');
                if (hasNegative) {
                    processed = '-' + processed;
                }
            } else {
                processed = processed.replace(/-/g, '');
            }

            // 修改后的前导零处理逻辑
            if (processed !== '0') {
                // 优化后的正则表达式处理带负号的情况
                processed = processed.replace(/^(-?)0+(?=[1-9])/, '$1');
                // 处理纯零的情况（如-000 → -0）
                if (/^-?0+$/.test(processed)) {
                    processed = processed.startsWith('-') ? '-0' : '0';
                }
            }

            // 处理小数点
            if (allowDecimal) {
                const parts = processed.split('.');
                if (parts.length > 1) {
                    // 保留第一个小数点，移除后续的小数点
                    processed = parts[0] + '.' + parts.slice(1).join('').replace(/\./g, '');
                }
                // 处理以小数点开头的情况
                if (processed.startsWith('.') && processed.length > 1) {
                    processed = '0' + processed;
                }
                // 处理负号后直接跟小数点的情况
                if (processed.startsWith('-.')) {
                    processed = '-0.' + processed.slice(2);
                }
            } else {
                processed = processed.replace(/\./g, '');
            }

            // 最终验证
            const finalReg = allowNegative 
                ? allowDecimal 
                    ? /^-?\d*\.?\d*$/
                    : /^-?\d*$/
                : allowDecimal
                    ? /^\d*\.?\d*$/
                    : /^\d*$/;

            if (!finalReg.test(processed)) {
                processed = '';
            }

            // 特殊允许状态
            if (allowNegative && processed === '-') {
                inputElement.value = '-';
                return;
            }
            if (allowDecimal && processed.endsWith('.') && !processed.startsWith('.') && processed !== '-.') {
                inputElement.value = processed;
                return;
            }

            // 设置最终值
            inputElement.value = processed || '';
        };

        const handleCompositionStart = () => {
            isComposing = true;
        };

        const handleCompositionEnd = () => {
            isComposing = false;
            handleInput({ target: inputElement });
        };

        inputElement.addEventListener('input', handleInput);
        inputElement.addEventListener('compositionstart', handleCompositionStart);
        inputElement.addEventListener('compositionend', handleCompositionEnd);
    },
    unmounted(el) {
        const inputElement = el.querySelector('input');

        const handleInput = (e) => {};
        const handleCompositionStart = () => {};
        const handleCompositionEnd = () => {};

        inputElement.removeEventListener('input', handleInput);
        inputElement.removeEventListener('compositionstart', handleCompositionStart);
        inputElement.removeEventListener('compositionend', handleCompositionEnd);
    }
}
