/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

import { __extends, __awaiter, __generator, __assign, __decorate, __metadata } from 'tslib';
import React__default from 'react';
import { findDOMNode } from 'react-dom';
import BaseTextArea from 'react-textarea-autosize';
import { ucFirst, autobind, themeable, localeable } from 'amis-core';
import { Icon } from './icons.js';

var Textarea = /** @class */ (function (_super) {
    __extends(Textarea, _super);
    function Textarea() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            focused: false
        };
        _this.inputRef = function (ref) {
            _this.input = findDOMNode(ref);
            if (_this.props.forwardRef) {
                _this.props.forwardRef.current = _this.input;
            }
            return _this.input;
        };
        return _this;
    }
    Textarea.prototype.valueToString = function (value) {
        return typeof value === 'undefined' || value === null
            ? ''
            : typeof value === 'string'
                ? value
                : JSON.stringify(value);
    };
    Textarea.prototype.focus = function () {
        var _this = this;
        if (!this.input) {
            return;
        }
        this.setState({
            focused: true
        }, function () {
            if (!_this.input) {
                return;
            }
            _this.input.focus();
            // 光标放到最后
            var len = _this.input.value.length;
            len && _this.input.setSelectionRange(len, len);
        });
    };
    Textarea.prototype.handleChange = function (e) {
        var onChange = this.props.onChange;
        var value = e.currentTarget.value;
        onChange === null || onChange === void 0 ? void 0 : onChange(value);
    };
    Textarea.prototype.handleFocus = function (e) {
        var onFocus = this.props.onFocus;
        this.setState({
            focused: true
        }, function () {
            onFocus === null || onFocus === void 0 ? void 0 : onFocus(e);
        });
    };
    Textarea.prototype.handleBlur = function (e) {
        var _a = this.props, onBlur = _a.onBlur; _a.trimContents; _a.value; _a.onChange;
        this.setState({
            focused: false
        }, function () {
            // 和renderer的重复了，不知道这里干啥的，先注释了
            // if (trimContents && value && typeof value === 'string') {
            //   onChange?.(value.trim());
            // }
            onBlur && onBlur(e);
        });
    };
    Textarea.prototype.handleClear = function () {
        return __awaiter(this, void 0, void 0, function () {
            var _a, onChange, resetValue;
            return __generator(this, function (_b) {
                _a = this.props, onChange = _a.onChange, resetValue = _a.resetValue;
                onChange === null || onChange === void 0 ? void 0 : onChange(resetValue);
                this.focus();
                return [2 /*return*/];
            });
        });
    };
    Textarea.prototype.render = function () {
        var _a;
        var _b = this.props, className = _b.className; _b.classPrefix; var value = _b.value, placeholder = _b.placeholder, disabled = _b.disabled, minRows = _b.minRows, maxRows = _b.maxRows, readOnly = _b.readOnly, name = _b.name, borderMode = _b.borderMode, cx = _b.classnames, maxLength = _b.maxLength, showCounter = _b.showCounter, clearable = _b.clearable, testIdBuilder = _b.testIdBuilder, style = _b.style;
        var counter = showCounter ? this.valueToString(value).length : 0;
        return (React__default.createElement("div", { className: cx("TextareaControl", (_a = {},
                _a["TextareaControl--border".concat(ucFirst(borderMode))] = borderMode,
                _a['is-focused'] = this.state.focused,
                _a['is-disabled'] = disabled || readOnly,
                _a), className), style: style },
            React__default.createElement(BaseTextArea, __assign({ className: cx("TextareaControl-input", {
                    'TextareaControl-input--counter': showCounter
                }), autoComplete: "off", ref: this.inputRef, name: name, disabled: disabled, value: this.valueToString(value), placeholder: placeholder, autoCorrect: "off", spellCheck: "false", maxLength: maxLength, readOnly: readOnly, minRows: minRows || undefined, maxRows: maxRows || undefined, onChange: this.handleChange, onFocus: this.handleFocus, onBlur: this.handleBlur }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getTestId())),
            clearable && !disabled && value ? (React__default.createElement("a", { onClick: this.handleClear, className: cx('TextareaControl-clear') },
                React__default.createElement(Icon, { icon: "input-clear", className: "icon" }))) : null,
            showCounter ? (React__default.createElement("span", { className: cx('TextareaControl-counter', {
                    'is-empty': counter === 0,
                    'is-clearable': clearable && !disabled && value
                }) },
                React__default.createElement("span", null, counter),
                typeof maxLength === 'number' && maxLength ? (React__default.createElement(React__default.Fragment, null,
                    React__default.createElement("i", null, "/"),
                    React__default.createElement("span", null, maxLength))) : null)) : null));
    };
    Textarea.defaultProps = {
        minRows: 3,
        maxRows: 20,
        trimContents: true,
        resetValue: '',
        clearable: false
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], Textarea.prototype, "handleChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], Textarea.prototype, "handleFocus", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], Textarea.prototype, "handleBlur", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", Promise)
    ], Textarea.prototype, "handleClear", null);
    return Textarea;
}(React__default.Component));
var Textarea$1 = themeable(localeable(Textarea));

export { Textarea, Textarea$1 as default };
