import { __assign, __read, __rest } from "tslib";
import React, { useRef, useState } from "react";
import InputBase from "../InputBase";
var Input = function (props) {
    var id = props.id, isAutoFocus = props.isAutoFocus, error = props.error, width = props.width, name = props.name, type = props.type, isDisabled = props.isDisabled, _a = props.isShowClearIcon, isShowClearIcon = _a === void 0 ? true : _a, placeholder = props.placeholder, tooltipContent = props.tooltipContent, tooltipPosition = props.tooltipPosition, maxLength = props.maxLength, title = props.title, onKeyDown = props.onKeyDown, alignText = props.alignText, isAutoFill = props.isAutoFill, className = props.className, inputMode = props.inputMode, pattern = props.pattern, value = props.value, onChange = props.onChange, onFocus = props.onFocus, onBlur = props.onBlur, onKeyPress = props.onKeyPress, _b = props.testId, testId = _b === void 0 ? "unknownTestId" : _b, isRequired = props.isRequired, size = props.size, status = props.status, statusText = props.statusText, iconLeft = props.iconLeft, iconRight = props.iconRight, _c = props.isShowLabel, isShowLabel = _c === void 0 ? true : _c, _d = props.viewType, viewType = _d === void 0 ? "round" : _d, isReadOnly = props.isReadOnly, _e = props.readOnlyEmptyText, readOnlyEmptyText = _e === void 0 ? "Не заполнено" : _e, otherProps = __rest(props, ["id", "isAutoFocus", "error", "width", "name", "type", "isDisabled", "isShowClearIcon", "placeholder", "tooltipContent", "tooltipPosition", "maxLength", "title", "onKeyDown", "alignText", "isAutoFill", "className", "inputMode", "pattern", "value", "onChange", "onFocus", "onBlur", "onKeyPress", "testId", "isRequired", "size", "status", "statusText", "iconLeft", "iconRight", "isShowLabel", "viewType", "isReadOnly", "readOnlyEmptyText"]);
    var inputTargetRef = useRef(null);
    var _f = __read(useState(false), 2), isFocused = _f[0], setFocused = _f[1];
    var _g = __read(useState(false), 2), isPasswordVisible = _g[0], setPasswordVisible = _g[1];
    var handleChange = function (event) {
        var _value = event.target.value;
        onChange && onChange(event, _value, id);
    };
    var handleFocus = function (event) {
        !isReadOnly && onFocus && onFocus(event);
        !isReadOnly && setFocused(true);
    };
    var handleBlur = function (event) {
        var _value = event.target.value;
        onBlur && onBlur(event, _value);
        setFocused(false);
    };
    var handleKeyPress = function (event) {
        onKeyPress && onKeyPress(event);
    };
    var handlePasswordToggle = function () {
        setPasswordVisible(!isPasswordVisible);
    };
    var handleClear = function (event) {
        event.preventDefault();
        event.stopPropagation();
        onChange && onChange(event, "", id);
    };
    var baseInputProps = {
        error: error,
        placeholder: placeholder,
        tooltipContent: tooltipContent,
        tooltipPosition: tooltipPosition,
        type: type,
        size: size,
        title: title,
        iconLeft: iconLeft,
        iconRight: iconRight,
        width: width,
        alignText: alignText,
        className: className,
        isDisabled: isDisabled,
        isShowClearIcon: isShowClearIcon,
        isFocused: isFocused,
        isPasswordVisible: isPasswordVisible,
        testId: testId,
        status: status,
        statusText: statusText,
        isHasValue: Boolean(value),
        onClear: handleClear,
        onPasswordToggle: handlePasswordToggle,
        isRequired: isRequired,
        isShowLabel: isShowLabel,
        viewType: viewType,
        isReadOnly: isReadOnly
    };
    var sharedProps = {
        id: id,
        maxLength: maxLength,
        name: name,
        autoFocus: isAutoFocus,
        value: !value && isReadOnly ? readOnlyEmptyText : value || "",
        inputMode: inputMode,
        onChange: handleChange,
        onKeyPress: handleKeyPress,
        onFocus: handleFocus,
        onBlur: handleBlur,
        onKeyDown: onKeyDown,
        pattern: pattern,
        status: status,
        disabled: isDisabled,
        readOnly: isReadOnly
    };
    return (React.createElement(InputBase, __assign({}, baseInputProps, otherProps),
        React.createElement("input", __assign({ "data-testid": "input_" + testId, "data-element": "input" }, sharedProps, { autoComplete: type === "password" || !isAutoFill ? "new-password" : "on", type: type === "password" && isPasswordVisible ? "text" : type, ref: inputTargetRef }))));
};
Input.defaultProps = {
    type: "text",
    size: "m",
    alignText: "left",
    isAutoFill: true,
    tooltipPosition: "bottom"
};
export default Input;
