"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _classnames = _interopRequireDefault(require("classnames"));
var _react = require("react");
var _constants = require("../../constants");
var _calculateNodeHeight = _interopRequireDefault(require("./calculateNodeHeight"));
var _jsxRuntime = require("react/jsx-runtime");
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

const getMinMax = rows => {
  if (typeof rows === 'number') {
    return {
      minRows: rows
    };
  } else if (typeof rows === 'object' && typeof rows.min === 'number' && typeof rows.max === 'number') {
    return {
      minRows: rows.min,
      maxRows: rows.max
    };
  }
  return null;
};
const AVAILABLE_SIZES = ['sm', 'md', 'lg', 'xl', 'fill'];

/**
 * `TextArea` is used for entering long text (like email body, descriptions, comments).
 * @since 1.9.0
 */

class TextArea extends _react.Component {
  constructor() {
    super(...arguments);
    (0, _defineProperty2.default)(this, "state", {
      textareaStyles: null
    });
    (0, _defineProperty2.default)(this, "rootRef", /*#__PURE__*/(0, _react.createRef)());
    (0, _defineProperty2.default)(this, "handleChange", e => {
      const {
        autoheight,
        onChange
      } = this.props;
      if (autoheight) {
        this.adjustHeight();
      }
      if (typeof onChange === 'function') {
        onChange(e);
      }
    });
  }
  static getDerivedStateFromProps(_ref) {
    let {
      rows
    } = _ref;
    if (rows) {
      return getMinMax(rows);
    }
    return null;
  }
  componentDidMount() {
    this.adjustHeight();
  }
  componentDidUpdate(prevProps) {
    const {
      autoheight
    } = this.props;
    if (autoheight !== prevProps.autoheight) {
      this.adjustHeight();
    }
  }
  adjustHeight() {
    const {
      autoheight
    } = this.props;
    if (!autoheight) {
      this.setState({
        textareaStyles: null
      });
      return;
    }
    const {
      minRows,
      maxRows
    } = this.state;
    if (this.rootRef.current) {
      const textareaStyles = (0, _calculateNodeHeight.default)(this.rootRef.current, false, minRows, maxRows);
      this.setState({
        textareaStyles
      });
    }
  }
  render() {
    const {
      baseClassName,
      className,
      rows,
      size,
      autoheight,
      onChange,
      ...other
    } = this.props;
    const {
      textareaStyles,
      minRows
    } = this.state;
    const useSizeModifier = size && AVAILABLE_SIZES.indexOf(size) !== -1;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("textarea", {
      className: (0, _classnames.default)(baseClassName, {
        [`${baseClassName}--size-${size}`]: useSizeModifier
      }, className),
      ref: this.rootRef,
      style: textareaStyles,
      onChange: this.handleChange,
      rows: minRows,
      ...other
    });
  }
}
(0, _defineProperty2.default)(TextArea, "defaultProps", {
  baseClassName: `${_constants.CLS_PREFIX}textarea`,
  className: null,
  rows: 4,
  size: 'md',
  autoheight: false,
  onChange: null
});
var _default = exports.default = TextArea;