"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
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 {};
};
const AVAILABLE_SIZES = new Set(['sm', 'md', 'lg', 'xl', 'fill']);

/**
 * `TextArea` is used for entering long text (like email body, descriptions, comments).
 * @since 1.9.0
 */
const TextArea = _ref => {
  let {
    baseClassName = `${_constants.CLS_PREFIX}textarea`,
    className,
    rows = 4,
    size = 'md',
    autoheight = false,
    onChange,
    style,
    ...props
  } = _ref;
  const rootRef = (0, _react.useRef)(null);
  const useSizeModifier = size && AVAILABLE_SIZES.has(size);
  const {
    minRows,
    maxRows
  } = getMinMax(rows);
  const [heightStyle, setHeightStyle] = (0, _react.useState)();
  const adjustHeight = (0, _react.useCallback)(() => {
    if (!autoheight || !rootRef.current) {
      setHeightStyle(undefined);
      return;
    }
    setHeightStyle((0, _calculateNodeHeight.default)(rootRef.current, false, minRows, maxRows));
  }, [autoheight, maxRows, minRows]);
  (0, _react.useLayoutEffect)(() => {
    adjustHeight();
  }, [adjustHeight]);
  const handleChange = e => {
    if (autoheight) {
      adjustHeight();
    }
    if (typeof onChange === 'function') {
      onChange(e);
    }
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("textarea", {
    ref: rootRef,
    className: (0, _classnames.default)(baseClassName, {
      [`${baseClassName}--size-${size}`]: useSizeModifier
    }, className),
    style: {
      ...style,
      ...heightStyle
    },
    onChange: handleChange,
    rows: minRows,
    ...props
  });
};
var _default = exports.default = TextArea;