"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Input = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const react_2 = require("@chakra-ui/react");
/**
 * Input component using Chakra UI's Input with BFUI theme customizations
 * Includes enhanced accessibility and performance optimizations
 */
const InputComponent = ({ label, error, helperText, size = 'md', isRequired = false, id, name, value, placeholder, onChange, type = 'text', isDisabled = false, isReadOnly = false, autoComplete, pattern, min, max, step, maxLength, minLength, 'aria-label': ariaLabel, 'aria-describedby': ariaDescribedBy, 'aria-invalid': ariaInvalid, leftElement, rightElement, autoFocus = false, onFocus, onBlur, 'data-testid': dataTestId, ...rest }) => {
    const inputRef = (0, react_1.useRef)(null);
    // Generate IDs for accessibility
    const inputId = (0, react_1.useMemo)(() => id || label?.toLowerCase().replace(/\s+/g, '-'), [id, label]);
    const errorId = (0, react_1.useMemo)(() => error ? `${inputId}-error` : undefined, [inputId, error]);
    const helperId = (0, react_1.useMemo)(() => helperText ? `${inputId}-helper` : undefined, [inputId, helperText]);
    // Compute aria-describedby
    const computedAriaDescribedBy = (0, react_1.useMemo)(() => {
        const parts = [];
        if (ariaDescribedBy)
            parts.push(ariaDescribedBy);
        if (errorId)
            parts.push(errorId);
        if (helperId)
            parts.push(helperId);
        return parts.length > 0 ? parts.join(' ') : undefined;
    }, [ariaDescribedBy, errorId, helperId]);
    // Auto-focus handling
    (0, react_1.useEffect)(() => {
        if (autoFocus && inputRef.current) {
            inputRef.current.focus();
        }
    }, [autoFocus]);
    // Memoize change handler
    const handleChange = (0, react_1.useCallback)((event) => {
        if (!isDisabled && !isReadOnly && onChange) {
            onChange(event);
        }
    }, [isDisabled, isReadOnly, onChange]);
    // Render input with or without input group based on elements
    const inputElement = (0, react_1.useMemo)(() => {
        const inputProps = {
            ref: inputRef,
            id: inputId,
            name: name || inputId,
            size,
            value,
            placeholder,
            onChange: handleChange,
            type,
            isDisabled,
            isReadOnly,
            autoComplete,
            pattern,
            min,
            max,
            step,
            maxLength,
            minLength,
            onFocus,
            onBlur,
            'aria-label': ariaLabel || label,
            'aria-describedby': computedAriaDescribedBy,
            'aria-invalid': ariaInvalid ?? (error ? 'true' : 'false'),
            'aria-required': isRequired,
            'data-testid': dataTestId,
            ...rest
        };
        if (leftElement || rightElement) {
            return ((0, jsx_runtime_1.jsxs)(react_2.InputGroup, { size: size, children: [leftElement && (0, jsx_runtime_1.jsx)(react_2.InputLeftElement, { children: leftElement }), (0, jsx_runtime_1.jsx)(react_2.Input, { ...inputProps }), rightElement && (0, jsx_runtime_1.jsx)(react_2.InputRightElement, { children: rightElement })] }));
        }
        return (0, jsx_runtime_1.jsx)(react_2.Input, { ...inputProps });
    }, [inputId, name, size, value, placeholder, handleChange, type, isDisabled, isReadOnly, autoComplete, pattern, min, max, step, maxLength, minLength, onFocus, onBlur, ariaLabel, label, computedAriaDescribedBy, ariaInvalid, error, isRequired, dataTestId, leftElement, rightElement, rest]);
    return ((0, jsx_runtime_1.jsxs)(react_2.FormControl, { isInvalid: !!error, isRequired: isRequired, children: [label && ((0, jsx_runtime_1.jsx)(react_2.FormLabel, { htmlFor: inputId, id: `${inputId}-label`, children: label })), inputElement, error ? ((0, jsx_runtime_1.jsx)(react_2.FormErrorMessage, { id: errorId, role: "alert", children: error })) : helperText ? ((0, jsx_runtime_1.jsx)(react_2.FormHelperText, { id: helperId, children: helperText })) : null] }));
};
// Memoize the component for better performance
exports.Input = (0, react_1.memo)(InputComponent);
exports.Input.displayName = 'Input';
exports.default = exports.Input;
//# sourceMappingURL=Input.js.map