(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "tslib", "react", "react-text-mask", "../hooks/useStateProps", "../InputBase"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var tslib_1 = require("tslib");
    var react_1 = (0, tslib_1.__importStar)(require("react"));
    var react_text_mask_1 = (0, tslib_1.__importDefault)(require("react-text-mask"));
    var useStateProps_1 = require("../hooks/useStateProps");
    var InputBase_1 = (0, tslib_1.__importDefault)(require("../InputBase"));
    var InputMask = (0, react_1.forwardRef)(function (props, ref) {
        var id = props.id, isAutoFocus = props.isAutoFocus, error = props.error, name = props.name, type = props.type, isDisabled = props.isDisabled, placeholder = props.placeholder, tooltipContent = props.tooltipContent, tooltipPosition = props.tooltipPosition, maxLength = props.maxLength, onIconLeftClick = props.onIconLeftClick, onIconRightClick = props.onIconRightClick, title = props.title, onKeyDown = props.onKeyDown, alignText = props.alignText, className = props.className, pattern = props.pattern, value = props.value, onChange = props.onChange, onFocus = props.onFocus, onBlur = props.onBlur, onKeyPress = props.onKeyPress, isShowMaskProp = props.isShowMask, isShowMaskOnFocus = props.isShowMaskOnFocus, isGuide = props.isGuide, pipe = props.pipe, placeholderChar = props.placeholderChar, isKeepCharPositions = props.isKeepCharPositions, mask = props.mask, testId = props.testId, isReadOnly = props.isReadOnly, isRequired = props.isRequired, _a = props.size, size = _a === void 0 ? "l" : _a, status = props.status, statusText = props.statusText, iconLeft = props.iconLeft, iconRight = props.iconRight, isShowClearIcon = props.isShowClearIcon, width = props.width, viewType = props.viewType, _b = props.readOnlyEmptyText, readOnlyEmptyText = _b === void 0 ? "Не заполнено" : _b, onClear = props.onClear, otherProps = (0, tslib_1.__rest)(props, ["id", "isAutoFocus", "error", "name", "type", "isDisabled", "placeholder", "tooltipContent", "tooltipPosition", "maxLength", "onIconLeftClick", "onIconRightClick", "title", "onKeyDown", "alignText", "className", "pattern", "value", "onChange", "onFocus", "onBlur", "onKeyPress", "isShowMask", "isShowMaskOnFocus", "isGuide", "pipe", "placeholderChar", "isKeepCharPositions", "mask", "testId", "isReadOnly", "isRequired", "size", "status", "statusText", "iconLeft", "iconRight", "isShowClearIcon", "width", "viewType", "readOnlyEmptyText", "onClear"]);
        var _c = (0, tslib_1.__read)((0, react_1.useState)(false), 2), isFocused = _c[0], setFocused = _c[1];
        var _d = (0, tslib_1.__read)((0, useStateProps_1.useStateProps)(value), 2), localValue = _d[0], setLocalValue = _d[1];
        var _e = (0, tslib_1.__read)((0, useStateProps_1.useStateProps)(isShowMaskProp === undefined ? false : isShowMaskProp), 2), isShowMask = _e[0], setShowMask = _e[1];
        var handleChange = function (event) {
            if (!isReadOnly && !isDisabled) {
                setLocalValue(event.target.value);
                onChange === null || onChange === void 0 ? void 0 : onChange(event, event.target.value, id);
            }
        };
        var handleFocus = function (event) {
            if (!isReadOnly) {
                onFocus === null || onFocus === void 0 ? void 0 : onFocus(event);
                isShowMaskOnFocus && setShowMask(true);
                setFocused(true);
            }
        };
        var handleBlur = function (event) {
            onBlur === null || onBlur === void 0 ? void 0 : onBlur(event, event.target.value);
            isShowMaskOnFocus && setShowMask(false);
            setFocused(false);
        };
        var handleKeyPress = function (event) {
            onKeyPress === null || onKeyPress === void 0 ? void 0 : onKeyPress(event);
        };
        var handleClear = function (event) {
            event.preventDefault();
            event.stopPropagation();
            if (!isDisabled && !isReadOnly) {
                setLocalValue("");
                onChange === null || onChange === void 0 ? void 0 : onChange(event, "");
                onClear === null || onClear === void 0 ? void 0 : onClear(event);
            }
        };
        var baseInputProps = {
            error: error,
            placeholder: placeholder,
            tooltipContent: tooltipContent,
            tooltipPosition: tooltipPosition,
            type: type,
            title: title,
            onIconLeftClick: onIconLeftClick,
            onIconRightClick: onIconRightClick,
            alignText: alignText,
            className: className,
            isDisabled: isDisabled || isReadOnly,
            isFocused: isFocused,
            isHasValue: Boolean(localValue),
            onClear: handleClear,
            isShowMask: isShowMask,
            testId: testId,
            isReadOnly: isReadOnly,
            isRequired: isRequired,
            size: size,
            status: status,
            statusText: statusText,
            iconLeft: iconLeft,
            iconRight: iconRight,
            isShowClearIcon: isShowClearIcon,
            isShowLabel: true,
            width: width,
            viewType: viewType
        };
        var isShowMaskComputed = (isShowMaskOnFocus && isFocused) || isShowMask;
        return (react_1.default.createElement(InputBase_1.default, (0, tslib_1.__assign)({ testId: testId }, baseInputProps, { ref: ref }, otherProps),
            react_1.default.createElement(react_text_mask_1.default, { id: id, maxLength: maxLength, name: name, autoFocus: isAutoFocus, value: isReadOnly && !localValue ? readOnlyEmptyText : localValue, onChange: handleChange, onKeyPress: handleKeyPress, onFocus: handleFocus, onBlur: handleBlur, onKeyDown: onKeyDown, showMask: isShowMaskComputed, guide: isShowMask ? isShowMask : isGuide, type: type, placeholderChar: placeholderChar, keepCharPositions: isKeepCharPositions, pattern: pattern, mask: isReadOnly && !localValue
                    ? readOnlyEmptyText.split("").map(function () { return /\S|\s/; })
                    : mask, pipe: pipe, disabled: isDisabled, readOnly: isReadOnly, ref: props.refMask })));
    });
    InputMask.displayName = "InputMask";
    exports.default = InputMask;
});
