import { jsx, jsxs } from "react/jsx-runtime";
import { cn } from "@uipath/apollo-wind";
import { memo, useCallback, useEffect, useRef, useState } from "react";
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("div", {
        className: "flex flex-col gap-1",
        children: [
            /*#__PURE__*/ jsx("label", {
                htmlFor: `field-${field.key}`,
                className: "text-[13px] text-(--canvas-foreground-de-emp)",
                children: field.label
            }),
            /*#__PURE__*/ jsxs("div", {
                className: "flex items-center gap-2",
                children: [
                    /*#__PURE__*/ jsx("input", {
                        id: `field-${field.key}`,
                        className: cn('nodrag flex-1 px-3 py-2 text-[13px] font-mono border border-(--canvas-border) rounded bg-(--canvas-background) text-(--canvas-foreground) outline-none transition-colors', 'focus:border-(--canvas-primary)', 'disabled:opacity-50 disabled:cursor-not-allowed', '[appearance:textfield] [&::-webkit-inner-spin-button]:hidden [&::-webkit-outer-spin-button]:hidden', error && 'border-(--canvas-error)'),
                        type: "number",
                        value: localValue,
                        onChange: handleChange,
                        placeholder: field.placeholder,
                        disabled: field.disabled,
                        min: field.min,
                        max: field.max,
                        step: field.step
                    }),
                    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 { NumberField_NumberField as NumberField };
