(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", "./styles", "./helpers/getSizingData", "./helpers/calculateNodeHeight"], 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 styles_1 = require("./styles");
    var getSizingData_1 = (0, tslib_1.__importDefault)(require("./helpers/getSizingData"));
    var calculateNodeHeight_1 = (0, tslib_1.__importDefault)(require("./helpers/calculateNodeHeight"));
    var TextArea = function (_a) {
        var id = _a.id, value = _a.value, onBlur = _a.onBlur, onChange = _a.onChange, onFocus = _a.onFocus, onKeyDown = _a.onKeyDown, onKeyPress = _a.onKeyPress, isAutoFocus = _a.isAutoFocus, isDisabled = _a.isDisabled, isRequired = _a.isRequired, error = _a.error, _b = _a.size, size = _b === void 0 ? "l" : _b, status = _a.status, statusText = _a.statusText, placeholder = _a.placeholder, testId = _a.testId, tooltipContent = _a.tooltipContent, tooltipPosition = _a.tooltipPosition, alignText = _a.alignText, className = _a.className, name = _a.name, maxLength = _a.maxLength, isShowClearIcon = _a.isShowClearIcon, rows = _a.rows, cols = _a.cols, _c = _a.minRows, minRows = _c === void 0 ? 1 : _c, _d = _a.maxRows, maxRows = _d === void 0 ? Infinity : _d, _e = _a.isAutoSize, isAutoSize = _e === void 0 ? false : _e, iconLeft = _a.iconLeft, statusLabel = _a.statusLabel, isReadOnly = _a.isReadOnly, _f = _a.readOnlyEmptyText, readOnlyEmptyText = _f === void 0 ? "Не заполнено" : _f, props = (0, tslib_1.__rest)(_a, ["id", "value", "onBlur", "onChange", "onFocus", "onKeyDown", "onKeyPress", "isAutoFocus", "isDisabled", "isRequired", "error", "size", "status", "statusText", "placeholder", "testId", "tooltipContent", "tooltipPosition", "alignText", "className", "name", "maxLength", "isShowClearIcon", "rows", "cols", "minRows", "maxRows", "isAutoSize", "iconLeft", "statusLabel", "isReadOnly", "readOnlyEmptyText"]);
        var _g = (0, tslib_1.__read)((0, react_1.useState)(false), 2), isFocused = _g[0], setFocused = _g[1];
        var textareaRef = (0, react_1.useRef)(null);
        var resizerRef = (0, react_1.useRef)(null);
        var heightRef = react_1.default.useRef(0);
        var measurementsCacheRef = react_1.default.useRef();
        var handleFocus = function (event) {
            onFocus && onFocus(event);
            setFocused(true);
        };
        var handleBlur = function (event) {
            var _value = event.target.value;
            onBlur && onBlur(event, _value);
            setFocused(false);
        };
        var handleClear = function (event) {
            event.preventDefault();
            event.stopPropagation();
            onChange === null || onChange === void 0 ? void 0 : onChange(event, "", id);
        };
        var handleChange = function (event) {
            onChange === null || onChange === void 0 ? void 0 : onChange(event, event.target.value, id);
        };
        var baseInputProps = (0, tslib_1.__assign)({ placeholder: placeholder, error: error, tooltipPosition: tooltipPosition, tooltipContent: tooltipContent, size: size, alignText: alignText, className: className, isDisabled: isDisabled, isShowClearIcon: isShowClearIcon, isAutoFocus: isAutoFocus, testId: testId, isFocused: isFocused, statusText: statusText, status: status, isHasValue: Boolean(value) || Boolean(isReadOnly), onClear: handleClear, isRequired: isRequired, isPasswordVisible: false, statusLabel: statusLabel, iconLeft: iconLeft, isReadOnly: isReadOnly }, props);
        var handleKeyDown = function (event) {
            onKeyDown === null || onKeyDown === void 0 ? void 0 : onKeyDown(event);
        };
        var resizeTextarea = function () {
            var textAreaNode = textareaRef.current;
            var resizerNode = resizerRef.current;
            if (textAreaNode && resizerNode) {
                var textAreaSizingData = measurementsCacheRef.current
                    ? measurementsCacheRef.current
                    : (0, getSizingData_1.default)(textAreaNode);
                var resizerSizingData = (0, getSizingData_1.default)(resizerNode);
                if (!textAreaSizingData || !resizerSizingData) {
                    return;
                }
                measurementsCacheRef.current = textAreaSizingData;
                var _a = (0, tslib_1.__read)((0, calculateNodeHeight_1.default)(textAreaSizingData, textAreaNode.value || textAreaNode.placeholder || "x", minRows, maxRows), 1), heightTextArea = _a[0];
                var _b = (0, tslib_1.__read)((0, calculateNodeHeight_1.default)(resizerSizingData, textAreaNode.value || textAreaNode.placeholder || "x", minRows, maxRows), 1), heightResizer = _b[0];
                if (heightResizer > heightTextArea || heightResizer < 30 || isAutoSize) {
                    resizerNode.style.setProperty("overflow-y", "hidden");
                }
                else {
                    resizerNode.style.setProperty("overflow-y", "visible");
                }
                if (heightRef.current !== heightTextArea) {
                    heightRef.current = heightTextArea;
                    textAreaNode.style.setProperty("height", heightTextArea + "px");
                    if ((isAutoSize || minRows > 1 || Number.isFinite(maxRows)) &&
                        resizerRef.current) {
                        resizerNode.style.setProperty("height", heightTextArea + "px");
                    }
                }
            }
        };
        (0, react_1.useLayoutEffect)(function () {
            resizeTextarea();
        });
        return (react_1.default.createElement(styles_1.TextareaWrapper, (0, tslib_1.__assign)({}, baseInputProps, { isShowLabel: true, cols: cols, classNameIconBox: "textarea__icon-box", classNameContent: "textarea__content", classNameAddonsRight: "textarea__resizer-wrapper" }),
            react_1.default.createElement(styles_1.TextAreaResizerWrapper, { size: size, ref: resizerRef, isDisabled: isDisabled, isReadOnly: isReadOnly },
                react_1.default.createElement(styles_1.TextareaStyled, { id: id, name: name, value: !value && isReadOnly ? readOnlyEmptyText : value || "", maxLength: maxLength, onFocus: handleFocus, onBlur: handleBlur, onKeyDown: handleKeyDown, onKeyPress: onKeyPress, onChange: handleChange, rows: rows, cols: cols, disabled: isDisabled, size: size, isShowClearIcon: isShowClearIcon && Boolean(value), ref: textareaRef, readOnly: isReadOnly }))));
    };
    exports.default = TextArea;
});
