import { jsx, jsxs } from "react/jsx-runtime";
import { cn } from "@uipath/apollo-wind";
import { memo, useCallback, useEffect, useRef, useState } from "react";
const inputBase = 'w-full px-3 py-2 text-[13px] font-mono border border-(--canvas-border) rounded bg-(--canvas-background) text-(--canvas-foreground) outline-none transition-colors placeholder:text-(--canvas-foreground-de-emp) focus:border-(--canvas-primary) disabled:opacity-50 disabled:cursor-not-allowed';
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
    ]);
    const inputId = `field-${field.key}`;
    return /*#__PURE__*/ jsxs("div", {
        className: "flex flex-col gap-1",
        children: [
            /*#__PURE__*/ jsx("label", {
                htmlFor: inputId,
                className: "text-[13px] text-(--canvas-foreground-de-emp)",
                children: field.label
            }),
            'textarea' === field.type ? /*#__PURE__*/ jsx("textarea", {
                id: inputId,
                className: cn('nodrag resize-y', inputBase, error && 'border-(--canvas-error)'),
                value: localValue,
                onChange: handleChange,
                placeholder: field.placeholder,
                disabled: field.disabled,
                rows: field.rows || 3
            }) : /*#__PURE__*/ jsxs("div", {
                className: "flex items-center gap-2",
                children: [
                    /*#__PURE__*/ jsx("input", {
                        id: inputId,
                        className: cn('nodrag flex-1', inputBase, error && 'border-(--canvas-error)'),
                        type: "text",
                        value: localValue,
                        onChange: handleChange,
                        placeholder: field.placeholder,
                        disabled: field.disabled
                    }),
                    field.suffix && /*#__PURE__*/ jsx("span", {
                        className: "shrink-0 text-[13px] text-(--canvas-foreground-de-emp) whitespace-nowrap",
                        children: field.suffix
                    })
                ]
            }),
            field.helpText && /*#__PURE__*/ jsx("span", {
                className: "text-[12px] text-(--canvas-foreground-de-emp) block",
                children: field.helpText
            }),
            error && /*#__PURE__*/ jsx("span", {
                className: "text-[12px] text-(--canvas-error-text) block",
                children: error
            })
        ]
    });
});
export { TextField_TextField as TextField };
