"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeField = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const personalDataSecurity_1 = require("../personalDataSecurity");
const Skeleton_1 = require("../Skeleton");
const constants_1 = require("./constants");
const hooks_1 = require("./hooks");
const ResendCodeButton_1 = require("./ResendCodeButton");
const styles_1 = require("./styles");
/**
 * Поле для ввода кода подтверждения
 */
exports.CodeField = (0, react_1.forwardRef)(({ label, resendTimeout = constants_1.RESEND_TIMEOUT_DEFAULT, codeLength, isAllowResendCode = false, onResendCode, initialValue, loading = false, disabled = false, isError = false, errorText = constants_1.ERROR_TEXT_DEFAULT, onChange: onFieldChange, onComplete, isAutoFocus = false, isHidePersonalData, }, ref) => {
    const { inputRefs, setFocusIndexNext, setFocusIndexPrevious, setBlur } = (0, hooks_1.useFocusInput)(codeLength, isAutoFocus);
    const { codeValue, onKeyDown, onKeyUp, onChange, onPaste, clearCodeValue } = (0, hooks_1.useCodeState)(initialValue, codeLength, setFocusIndexNext, setFocusIndexPrevious, setBlur, onFieldChange, onComplete);
    const setRef = (index) => (el) => {
        if (el) {
            inputRefs.current[index] = el;
        }
    };
    const hidePersonalDataClassname = (0, personalDataSecurity_1.useHidePersonalData)({
        isEnabled: isHidePersonalData,
        type: 'input',
    });
    return ((0, jsx_runtime_1.jsxs)(styles_1.Wrapper, { ref: ref, children: [label && (0, jsx_runtime_1.jsx)(styles_1.FieldLabel, { children: label }), (0, jsx_runtime_1.jsx)(styles_1.DigitsWrapper, { children: codeValue.map((value, index) => (
                // biome-ignore lint/suspicious/noArrayIndexKey: Элементы статичны
                (0, jsx_runtime_1.jsx)(styles_1.DigitsItem, { children: loading ? ((0, jsx_runtime_1.jsx)(Skeleton_1.Skeleton, { variant: "rounded", height: 60, children: (0, jsx_runtime_1.jsx)(styles_1.Digit, { disabled: true }) })) : ((0, jsx_runtime_1.jsx)(styles_1.Digit, { className: hidePersonalDataClassname, "$isError": isError, type: "text", inputMode: "numeric", pattern: "[0-9]", disabled: disabled, value: value ?? '', ref: setRef(index), onKeyUp: (e) => onKeyUp(e, index), onKeyDown: (e) => onKeyDown(e, index), onChange: (e) => onChange(e, index), onPaste: (e) => !disabled && onPaste(e) })) }, index))) }), isError && (0, jsx_runtime_1.jsx)(styles_1.StyledHelperText, { error: true, children: errorText }), isAllowResendCode && ((0, jsx_runtime_1.jsx)(ResendCodeButton_1.ResendCodeButton, { resendTimeout: resendTimeout, disabled: disabled, loading: loading, onResendCode: onResendCode, clearCodeValue: clearCodeValue }))] }));
});
