"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MaskField = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const react_imask_1 = require("react-imask");
const styles_1 = require("./styles");
const MaskedTextField = (0, react_imask_1.IMaskMixin)(({ ...props }) => {
    const textFieldProps = props;
    return (0, jsx_runtime_1.jsx)(styles_1.StyledTextField, { ...textFieldProps });
});
exports.MaskField = (0, react_1.forwardRef)(({ onChange, onAccept, ...props }, ref) => {
    const maskFieldProps = { unmask: true, ...props };
    const handleMaskFieldAccept = (value, maskRef, e) => {
        if (onAccept) {
            return onAccept(value, maskRef, e, onChange);
        }
        onChange?.(value);
    };
    const handleOnChange = (event) => {
        // Вызов onChange чинит валидацию пустого поля на onBlur внутри Wizard
        if (event.target.value === '') {
            onChange?.(event.target.value);
        }
    };
    // фикс фокуса при клике на submit формы
    // (достигается путем вызова ref)
    const newRef = (instance) => {
        if (typeof ref === 'function') {
            ref(instance);
        }
        else if (ref) {
            ref.current = instance;
        }
    };
    return ((0, jsx_runtime_1.jsx)(MaskedTextField, { ...maskFieldProps, inputRef: newRef, onAccept: handleMaskFieldAccept, onChange: handleOnChange }));
});
