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, TextArea, TextInput } from "../NodePropertiesPanel.styles.js";
const TextField_TextField = /*#__PURE__*/ memo(function({ field, value, onChange, error }) {
    const [localValue, setLocalValue] = useState(value || '');
    const debounceTimer = useRef(null);
    useEffect(()=>{
        setLocalValue(value || '');
    }, [
        value
    ]);
    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(()=>{
            onChange(newValue);
            debounceTimer.current = null;
        }, debounceDelay);
        else onChange(newValue);
    }, [
        field.debounce,
        onChange
    ]);
    return /*#__PURE__*/ jsxs(Column, {
        gap: 4,
        children: [
            /*#__PURE__*/ jsx(FieldLabel, {
                children: field.label
            }),
            'textarea' === field.type ? /*#__PURE__*/ jsx(TextArea, {
                className: "nodrag",
                value: localValue,
                onChange: handleChange,
                placeholder: field.placeholder,
                disabled: field.disabled,
                rows: field.rows || 3,
                hasError: !!error
            }) : /*#__PURE__*/ jsxs(InputWrapper, {
                children: [
                    /*#__PURE__*/ jsx(TextInput, {
                        className: "nodrag",
                        type: "text",
                        value: localValue,
                        onChange: handleChange,
                        placeholder: field.placeholder,
                        disabled: field.disabled,
                        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 { TextField_TextField as TextField };
