import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _get from "@babel/runtime/helpers/get";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import { __decorate } from "tslib";
import React from 'react';
import { observer } from 'mobx-react';
import { action, observable, runInAction } from 'mobx';
import isString from 'lodash/isString';
import ReactResizeObserver from '../../../es/_util/resizeObserver';
import { TextField } from '../text-field/TextField';
import { ResizeType } from './enum';
import calculateNodeHeight from './calculateNodeHeight';
import autobind from '../_util/autobind';

function getResizeProp(resize) {
  switch (resize) {
    case ResizeType.both:
      return 'both';

    case ResizeType.vertical:
      return 'height';

    case ResizeType.horizontal:
      return 'width';

    default:
      return undefined;
  }
}

var TextArea = /*#__PURE__*/function (_TextField) {
  _inherits(TextArea, _TextField);

  var _super = _createSuper(TextArea);

  function TextArea() {
    _classCallCheck(this, TextArea);

    return _super.apply(this, arguments);
  }

  _createClass(TextArea, [{
    key: "multiple",
    get: function get() {
      return false;
    }
  }, {
    key: "resize",
    get: function get() {
      var _this$props = this.props,
          autoSize = _this$props.autoSize,
          resize = _this$props.resize;

      if (autoSize) {
        var minRows = autoSize.minRows,
            maxRows = autoSize.maxRows;

        if (minRows && minRows === maxRows) {
          return undefined;
        }
      }

      return resize;
    }
  }, {
    key: "autoSize",
    get: function get() {
      var autoSize = this.props.autoSize;

      if (autoSize) {
        var minRows = autoSize.minRows,
            maxRows = autoSize.maxRows;

        if (minRows && minRows === maxRows) {
          return false;
        }
      }

      return autoSize;
    }
  }, {
    key: "rows",
    get: function get() {
      var _this$props2 = this.props,
          _this$props2$rows = _this$props2.rows,
          rows = _this$props2$rows === void 0 ? 4 : _this$props2$rows,
          autoSize = _this$props2.autoSize;

      if (autoSize) {
        var minRows = autoSize.minRows;
        return minRows || rows;
      }

      return rows;
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      _get(_getPrototypeOf(TextArea.prototype), "componentDidMount", this).call(this);

      if (this.element && this.autoSize) {
        // 自适应高度，挂载时渲染样式
        this.forceUpdate();
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      var _this = this;

      var element = this.element,
          autoSize = this.autoSize;

      if (autoSize && element) {
        var minRows = autoSize.minRows,
            maxRows = autoSize.maxRows;
        var calculateStyle = calculateNodeHeight(element, true, minRows, maxRows);
        var autoSizeCalcHeight = this.autoSizeCalcHeight;

        if (!autoSizeCalcHeight || calculateStyle.height !== autoSizeCalcHeight.height) {
          runInAction(function () {
            _this.autoSizeCalcHeight = calculateStyle;
          });
        }
      }
    }
  }, {
    key: "getOmitPropsKeys",
    value: function getOmitPropsKeys() {
      return _get(_getPrototypeOf(TextArea.prototype), "getOmitPropsKeys", this).call(this).concat(['resize', 'autoSize', 'onResize']);
    }
  }, {
    key: "getOtherProps",
    value: function getOtherProps() {
      var _this$resize = this.resize,
          resize = _this$resize === void 0 ? ResizeType.none : _this$resize,
          autoSize = this.autoSize,
          rows = this.rows;

      var otherProps = _get(_getPrototypeOf(TextArea.prototype), "getOtherProps", this).call(this);

      var _otherProps$style = otherProps.style,
          style = _otherProps$style === void 0 ? {} : _otherProps$style;
      style.resize = resize;

      if (resize !== ResizeType.none) {
        style.transition = 'none';
      }

      if (autoSize) {
        var autoSizeCalcHeight = this.autoSizeCalcHeight;

        if (autoSizeCalcHeight) {
          _extends(style, autoSizeCalcHeight);
        }
      }

      otherProps.rows = rows;
      otherProps.style = style;
      return otherProps;
    }
  }, {
    key: "handleResize",
    value: function handleResize(width, height, target) {
      var onResize = this.props.onResize;

      if (!this.resized) {
        var element = this.element;

        if (element && element.style.width) {
          this.resized = true;
        }
      }

      if (onResize) {
        onResize(width, height, target);
      }
    }
  }, {
    key: "getSuffix",
    value: function getSuffix() {
      return null;
    }
  }, {
    key: "renderLengthInfoWrapper",
    value: function renderLengthInfoWrapper() {
      if (!this.showLengthInfo) {
        return null;
      }

      var editorTextInfo = this.getEditorTextInfo();
      var inputLength = editorTextInfo.text.length;
      var maxLength = this.getProp('maxLength');
      var lengthElement = this.renderLengthInfo(maxLength, inputLength);
      return lengthElement;
    }
  }, {
    key: "handleClearButtonClick",
    value: function handleClearButtonClick(e) {
      e.preventDefault();
      this.clear();
    }
  }, {
    key: "handleInnerButtonMouseDown",
    value: function handleInnerButtonMouseDown(e) {
      e.preventDefault();
    }
  }, {
    key: "renderWrapper",
    value: function renderWrapper() {
      var _this$resize2 = this.resize,
          resize = _this$resize2 === void 0 ? ResizeType.none : _this$resize2;
      var resizable = resize !== ResizeType.none;
      var wrapperProps = this.getWrapperProps() || {};
      var lengthElement = this.renderLengthInfoWrapper(); // 先计算suffix，然后再计算clearButton，设置right，避免重叠

      var suffix = this.getSuffix();
      var button = this.getInnerSpanButton();

      if (this.resized) {
        var wrapperStyle = wrapperProps.style;
        wrapperProps.style = _objectSpread(_objectSpread({}, wrapperStyle), {}, {
          width: 'auto'
        });
      }

      if (lengthElement) {
        var _wrapperStyle = wrapperProps.style;
        wrapperProps.style = _objectSpread(_objectSpread({}, _wrapperStyle), {}, {
          marginBottom: '0.2rem'
        });
      }

      var renderedValue = this.renderRenderedValue(undefined, {
        className: "".concat(this.prefixCls, "-renderer-wrapper")
      });
      var element = this.wrapperInputNode(renderedValue);
      var children = /*#__PURE__*/React.createElement(React.Fragment, null, element, lengthElement, suffix, button);
      return /*#__PURE__*/React.createElement("div", _extends({
        key: "wrapper"
      }, wrapperProps), this.renderPlaceHolder(), renderedValue, /*#__PURE__*/React.createElement("label", null, resizable ? /*#__PURE__*/React.createElement(ReactResizeObserver, {
        onResize: this.handleResize,
        resizeProp: getResizeProp(resize)
      }, children) : children, this.renderFloatLabel()));
    }
  }, {
    key: "wrapperInputNode",
    value: function wrapperInputNode(renderedValue) {
      var text = this.getTextNode();
      var elementProps = this.getOtherProps() || {};
      var otherProps = {};

      if (renderedValue) {
        otherProps.style = _objectSpread(_objectSpread({}, elementProps.style), {}, {
          textIndent: -1000,
          color: 'transparent'
        });
      }

      return /*#__PURE__*/React.createElement("textarea", _extends({}, elementProps, otherProps, {
        placeholder: this.hasFloatLabel && !this.isFocused ? undefined : this.getPlaceholders()[0],
        readOnly: !this.editable,
        value: isString(text) ? text : this.processValue(this.getValue())
      }));
    }
  }, {
    key: "handleEnterDown",
    value: function handleEnterDown(_) {// noop
    }
  }]);

  return TextArea;
}(TextField);

TextArea.displayName = 'TextArea';
TextArea.defaultProps = _objectSpread(_objectSpread({}, TextField.defaultProps), {}, {
  suffixCls: 'textarea',
  autoSize: false
}); // eslint-disable-next-line camelcase

TextArea.__PRO_TEXTAREA = true;

__decorate([observable], TextArea.prototype, "resized", void 0);

__decorate([observable], TextArea.prototype, "autoSizeCalcHeight", void 0);

__decorate([autobind, action], TextArea.prototype, "handleResize", null);

__decorate([autobind], TextArea.prototype, "handleClearButtonClick", null);

TextArea = __decorate([observer], TextArea);
export default TextArea;
//# sourceMappingURL=TextArea.js.map
