/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var isNull = require('lodash/isNull');
var _common_js_utils_helper = require('./helper.js');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('@babel/runtime/helpers/defineProperty');
require('lodash/isString');
require('lodash/isUndefined');
require('lodash/isNumber');
require('lodash/isArray');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var isNull__default = /*#__PURE__*/_interopDefaultLegacy(isNull);

var TEXTAREA_STYLE = "\n  min-height:0 !important;\n  max-height:none !important;\n  height:0 !important;\n  visibility:hidden !important;\n  overflow:hidden !important;\n  position:absolute !important;\n  z-index:-1000 !important;\n  top:0 !important;\n  right:0 !important\n";
var hiddenTextarea;
function calcTextareaHeight(targetElement) {
  var _hiddenTextarea, _hiddenTextarea$paren;
  var minRows = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  var maxRows = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  if (!hiddenTextarea) {
    hiddenTextarea = document.createElement("textarea");
    document.body.appendChild(hiddenTextarea);
  }
  var _calculateNodeSize = _common_js_utils_helper.calculateNodeSize(targetElement),
    paddingSize = _calculateNodeSize.paddingSize,
    borderSize = _calculateNodeSize.borderSize,
    boxSizing = _calculateNodeSize.boxSizing,
    sizingStyle = _calculateNodeSize.sizingStyle;
  hiddenTextarea.setAttribute("style", "".concat(sizingStyle, ";").concat(TEXTAREA_STYLE));
  hiddenTextarea.value = targetElement.value || targetElement.placeholder || "";
  var height = hiddenTextarea.scrollHeight;
  var result = {};
  var isBorderbox = boxSizing === "border-box";
  var isContentbox = boxSizing === "content-box";
  if (isBorderbox) height += borderSize;else if (isContentbox) height -= paddingSize;
  hiddenTextarea.value = "";
  var singleRowHeight = hiddenTextarea.scrollHeight - paddingSize;
  (_hiddenTextarea = hiddenTextarea) === null || _hiddenTextarea === void 0 ? void 0 : (_hiddenTextarea$paren = _hiddenTextarea.parentNode) === null || _hiddenTextarea$paren === void 0 ? void 0 : _hiddenTextarea$paren.removeChild(hiddenTextarea);
  hiddenTextarea = null;
  var calcHeight = function calcHeight(rows) {
    var rowsHeight = singleRowHeight * rows;
    if (isBorderbox) rowsHeight = rowsHeight + paddingSize + borderSize;
    return rowsHeight;
  };
  if (!isNull__default["default"](minRows)) {
    var minHeight = calcHeight(minRows);
    height = Math.max(minHeight, height);
    result.minHeight = "".concat(minHeight, "px");
  }
  if (!isNull__default["default"](maxRows)) height = Math.min(calcHeight(maxRows), height);
  result.height = "".concat(height, "px");
  return result;
}

exports["default"] = calcTextareaHeight;
//# sourceMappingURL=calcTextareaHeight.js.map
