import { defineComponent, ref, onMounted, watch } from 'vue';

// 假设 CurrencyBase 的类型定义
interface CurrencyBase {
    _initTxtLength: number;
    _formatType: string;
    _txtValue: string;
    onPreviewKeyDown?: (e: KeyboardEvent) => void;
    onTextChanged?: (e: Event) => void;
    onGotFocus?: (e: FocusEvent) => void;
    onLostFocus?: (e: FocusEvent) => void;
}
//TODO这里方法重复了需要处理
// 模拟 CurrencyBase 类
class CurrencyBase {
    _initTxtLength: number = 0;
    _formatType: string = '';
    _txtValue: string = '';

    // onPreviewKeyDown(e: KeyboardEvent) {
    //     // 基类默认处理逻辑
    // }

    // onTextChanged(e: Event) {
    //     // 基类默认处理逻辑
    // }

    // onGotFocus(e: FocusEvent) {
    //     // 基类默认处理逻辑
    // }

    // onLostFocus(e: FocusEvent) {
    //     // 基类默认处理逻辑
    // }
}

export default defineComponent({
    name: 'CurrencyEditor',
    setup() {
        const text = ref('0.00');
        const base = new CurrencyBase();

        onMounted(() => {
            // 模拟禁用输入法
            // 在 Web 中没有直接禁用输入法的方法，可通过 inputmode 属性限制输入
        });

        const handleKeyDown = (e: KeyboardEvent) => {
            base._initTxtLength = 4;
            base._formatType = '{0:n}';
            base.onPreviewKeyDown?.(e);
        };

        const handleTextChange = (e: Event) => {
            base._txtValue = '0.00';
            base._formatType = '{0:n}';
            base.onTextChanged?.(e);
        };

        const handleGotFocus = (e: FocusEvent) => {
            base._txtValue = '0.00';
            base.onGotFocus?.(e);
        };

        const handleLostFocus = (e: FocusEvent) => {
            base._txtValue = '0.00';
            base._formatType = '{0:n}';
            base.onLostFocus?.(e);
        };

        watch(text, (newValue) => {
            // 模拟文本更改事件
            const event = new Event('input');
            handleTextChange(event);
        });

        return {
            text,
            handleKeyDown,
            handleGotFocus,
            handleLostFocus
        };
    },
    template: `
        <input
            :value="text"
            @keydown="handleKeyDown"
            @focus="handleGotFocus"
            @blur="handleLostFocus"
            inputmode="numeric"
        />
    `
});
