import { jsx, jsxs } from "react/jsx-runtime";
import { Column } from "../../../layouts/index.js";
import { memo, useCallback, useEffect, useRef, useState } from "react";
import { FieldError, FieldHelpText, FieldLabel, InputSuffix, InputWrapper, NumberInput } from "../NodePropertiesPanel.styles.js";
const NumberField_NumberField = /*#__PURE__*/ memo(function({ field, value, onChange, error }) {
    const [localValue, setLocalValue] = useState(value ?? field.defaultValue ?? '');
    const debounceTimer = useRef(null);
    useEffect(()=>{
        setLocalValue(value ?? field.defaultValue ?? '');
    }, [
        value,
        field.defaultValue
    ]);
    useEffect(()=>()=>{
            if (debounceTimer.current) {
                clearTimeout(debounceTimer.current);
                debounceTimer.current = null;
            }
        }, []);
    const handleChange = useCallback((e)=>{
        const newValue = e.target.value;
        setLocalValue(newValue);
        if (debounceTimer.current) {
            clearTimeout(debounceTimer.current);
            debounceTimer.current = null;
        }
        const debounceDelay = field.debounce || 0;
        if (debounceDelay > 0) debounceTimer.current = setTimeout(()=>{
            const numValue = '' === newValue ? void 0 : Number(newValue);
            onChange(numValue);
            debounceTimer.current = null;
        }, debounceDelay);
        else {
            const numValue = '' === newValue ? void 0 : Number(newValue);
            onChange(numValue);
        }
    }, [
        field.debounce,
        onChange
    ]);
    return /*#__PURE__*/ jsxs(Column, {
        gap: 4,
        children: [
            /*#__PURE__*/ jsx(FieldLabel, {
                children: field.label
            }),
            /*#__PURE__*/ jsxs(InputWrapper, {
                children: [
                    /*#__PURE__*/ jsx(NumberInput, {
                        className: "nodrag",
                        type: "number",
                        value: localValue,
                        onChange: handleChange,
                        placeholder: field.placeholder,
                        disabled: field.disabled,
                        min: field.min,
                        max: field.max,
                        step: field.step,
                        hasError: !!error,
                        style: {
                            flex: 1
                        }
                    }),
                    field.suffix && /*#__PURE__*/ jsx(InputSuffix, {
                        children: field.suffix
                    })
                ]
            }),
            field.helpText && /*#__PURE__*/ jsx(FieldHelpText, {
                children: field.helpText
            }),
            error && /*#__PURE__*/ jsx(FieldError, {
                children: error
            })
        ]
    });
});
export { NumberField_NumberField as NumberField };
