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

import isNull from 'lodash/isNull';
import { calculateNodeSize } from './helper.js';
import '@babel/runtime/helpers/toConsumableArray';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import '@babel/runtime/helpers/defineProperty';
import 'lodash/isString';
import 'lodash/isUndefined';
import 'lodash/isNumber';
import 'lodash/isArray';

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 = 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(minRows)) {
    var minHeight = calcHeight(minRows);
    height = Math.max(minHeight, height);
    result.minHeight = "".concat(minHeight, "px");
  }
  if (!isNull(maxRows)) height = Math.min(calcHeight(maxRows), height);
  result.height = "".concat(height, "px");
  return result;
}

export { calcTextareaHeight as default };
//# sourceMappingURL=calcTextareaHeight.js.map
